找到最大开销的子字符串
难度:
标签:
题目描述
You are given a string s
, a string chars
of distinct characters and an integer array vals
of the same length as chars
.
The cost of the substring is the sum of the values of each character in the substring. The cost of an empty string is considered 0
.
The value of the character is defined in the following way:
- If the character is not in the string
chars
, then its value is its corresponding position (1-indexed) in the alphabet.- For example, the value of
'a'
is1
, the value of'b'
is2
, and so on. The value of'z'
is26
.
- For example, the value of
- Otherwise, assuming
i
is the index where the character occurs in the stringchars
, then its value isvals[i]
.
Return the maximum cost among all substrings of the string s
.
Example 1:
Input: s = "adaa", chars = "d", vals = [-1000] Output: 2 Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively. The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2. It can be proven that 2 is the maximum cost.
Example 2:
Input: s = "abc", chars = "abc", vals = [-1,-1,-1] Output: 0 Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively. The substring with the maximum cost is the empty substring "" and its cost is 0. It can be proven that 0 is the maximum cost.
Constraints:
1 <= s.length <= 105
s
consist of lowercase English letters.1 <= chars.length <= 26
chars
consist of distinct lowercase English letters.vals.length == chars.length
-1000 <= vals[i] <= 1000
代码结果
运行时间: 81 ms, 内存: 16.5 MB
/*
* 思路:
* 1. 创建一个哈希映射,用于存储 chars 中字符对应的 vals 值。
* 2. 使用流操作遍历字符串 s,对于每个字符,获取其对应的价值(在哈希映射中或通过其字母表位置计算)。
* 3. 使用流操作找到最大子数组和,该和即为最大开销。
*/
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
public class Solution {
public int maximumCostSubstring(String s, String chars, int[] vals) {
Map<Character, Integer> charValueMap = new HashMap<>();
for (int i = 0; i < chars.length(); i++) {
charValueMap.put(chars.charAt(i), vals[i]);
}
int[] maxCost = {0};
int[] currentCost = {0};
s.chars().forEach(c -> {
int value = charValueMap.getOrDefault((char) c, c - 'a' + 1);
currentCost[0] = Math.max(value, currentCost[0] + value);
maxCost[0] = Math.max(maxCost[0], currentCost[0]);
});
return maxCost[0];
}
}
解释
方法:
The solution implements the Kadane's algorithm to find the maximum sum of a contiguous subarray, where the array elements are the costs of characters in the string 's'. Initially, a dictionary 'd' is created to map each lowercase letter to its alphabetical index. This mapping is then updated with the given 'chars' and their corresponding 'vals'. During the iteration over the string 's', the value of each character is retrieved from the dictionary 'd', and the current sum 'cur' is updated. If 'cur' drops below zero, it is reset to zero. The maximum observed 'cur' is tracked with 'res'. This ensures that we are always considering the maximum cost of any contiguous substring.
时间复杂度:
O(n)
空间复杂度:
O(1)
代码细节讲解
🦆
为什么在计算每个字符的价值时,选择使用Kadane's algorithm来处理子字符串的最大开销?
▷🦆
在实现中,当当前累积开销`cur`小于0时,直接将其重置为0,这种处理方式的理论依据是什么?
▷🦆
你如何确定字典`d`的更新(使用chars和vals)不会引入重复的键值对,从而影响结果的准确性?
▷🦆
如果`chars`包含的字符和`vals`数组的长度不匹配,解决方案中会如何处理这种情况?
▷