comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1422 |
第 101 场双周赛 Q2 |
|
给你一个字符串 s
,一个字符 互不相同 的字符串 chars
和一个长度与 chars
相同的整数数组 vals
。
子字符串的开销 是一个子字符串中所有字符对应价值之和。空字符串的开销是 0
。
字符的价值 定义如下:
- 如果字符不在字符串
chars
中,那么它的价值是它在字母表中的位置(下标从 1 开始)。<ul> <li>比方说,<code>'a'</code> 的价值为 <code>1</code> ,<code>'b'</code> 的价值为 <code>2</code> ,以此类推,<code>'z'</code> 的价值为 <code>26</code> 。</li> </ul> </li> <li>否则,如果这个字符在 <code>chars</code> 中的位置为 <code>i</code> ,那么它的价值就是 <code>vals[i]</code> 。</li>
请你返回字符串 s
的所有子字符串中的最大开销。
示例 1:
输入:s = "adaa", chars = "d", vals = [-1000] 输出:2 解释:字符 "a" 和 "d" 的价值分别为 1 和 -1000 。 最大开销子字符串是 "aa" ,它的开销为 1 + 1 = 2 。 2 是最大开销。
示例 2:
输入:s = "abc", chars = "abc", vals = [-1,-1,-1] 输出:0 解释:字符 "a" ,"b" 和 "c" 的价值分别为 -1 ,-1 和 -1 。 最大开销子字符串是 "" ,它的开销为 0 。 0 是最大开销。
提示:
1 <= s.length <= 105
s
只包含小写英文字母。1 <= chars.length <= 26
chars
只包含小写英文字母,且 互不相同 。vals.length == chars.length
-1000 <= vals[i] <= 1000
我们根据题目描述,遍历字符串
遍历结束后返回答案
时间复杂度
class Solution:
def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:
d = {c: v for c, v in zip(chars, vals)}
ans = tot = mi = 0
for c in s:
v = d.get(c, ord(c) - ord('a') + 1)
tot += v
ans = max(ans, tot - mi)
mi = min(mi, tot)
return ans
class Solution {
public int maximumCostSubstring(String s, String chars, int[] vals) {
int[] d = new int[26];
for (int i = 0; i < d.length; ++i) {
d[i] = i + 1;
}
int m = chars.length();
for (int i = 0; i < m; ++i) {
d[chars.charAt(i) - 'a'] = vals[i];
}
int ans = 0, tot = 0, mi = 0;
int n = s.length();
for (int i = 0; i < n; ++i) {
int v = d[s.charAt(i) - 'a'];
tot += v;
ans = Math.max(ans, tot - mi);
mi = Math.min(mi, tot);
}
return ans;
}
}
class Solution {
public:
int maximumCostSubstring(string s, string chars, vector<int>& vals) {
vector<int> d(26);
iota(d.begin(), d.end(), 1);
int m = chars.size();
for (int i = 0; i < m; ++i) {
d[chars[i] - 'a'] = vals[i];
}
int ans = 0, tot = 0, mi = 0;
for (char& c : s) {
int v = d[c - 'a'];
tot += v;
ans = max(ans, tot - mi);
mi = min(mi, tot);
}
return ans;
}
};
func maximumCostSubstring(s string, chars string, vals []int) (ans int) {
d := [26]int{}
for i := range d {
d[i] = i + 1
}
for i, c := range chars {
d[c-'a'] = vals[i]
}
tot, mi := 0, 0
for _, c := range s {
v := d[c-'a']
tot += v
ans = max(ans, tot-mi)
mi = min(mi, tot)
}
return
}
function maximumCostSubstring(s: string, chars: string, vals: number[]): number {
const d: number[] = Array.from({ length: 26 }, (_, i) => i + 1);
for (let i = 0; i < chars.length; ++i) {
d[chars.charCodeAt(i) - 97] = vals[i];
}
let ans = 0;
let tot = 0;
let mi = 0;
for (const c of s) {
tot += d[c.charCodeAt(0) - 97];
ans = Math.max(ans, tot - mi);
mi = Math.min(mi, tot);
}
return ans;
}
我们可以将每个字符
我们用变量
时间复杂度
class Solution:
def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:
d = {c: v for c, v in zip(chars, vals)}
ans = f = 0
for c in s:
v = d.get(c, ord(c) - ord('a') + 1)
f = max(f, 0) + v
ans = max(ans, f)
return ans
class Solution {
public int maximumCostSubstring(String s, String chars, int[] vals) {
int[] d = new int[26];
for (int i = 0; i < d.length; ++i) {
d[i] = i + 1;
}
int m = chars.length();
for (int i = 0; i < m; ++i) {
d[chars.charAt(i) - 'a'] = vals[i];
}
int ans = 0, f = 0;
int n = s.length();
for (int i = 0; i < n; ++i) {
int v = d[s.charAt(i) - 'a'];
f = Math.max(f, 0) + v;
ans = Math.max(ans, f);
}
return ans;
}
}
class Solution {
public:
int maximumCostSubstring(string s, string chars, vector<int>& vals) {
vector<int> d(26);
iota(d.begin(), d.end(), 1);
int m = chars.size();
for (int i = 0; i < m; ++i) {
d[chars[i] - 'a'] = vals[i];
}
int ans = 0, f = 0;
for (char& c : s) {
int v = d[c - 'a'];
f = max(f, 0) + v;
ans = max(ans, f);
}
return ans;
}
};
func maximumCostSubstring(s string, chars string, vals []int) (ans int) {
d := [26]int{}
for i := range d {
d[i] = i + 1
}
for i, c := range chars {
d[c-'a'] = vals[i]
}
f := 0
for _, c := range s {
v := d[c-'a']
f = max(f, 0) + v
ans = max(ans, f)
}
return
}
function maximumCostSubstring(s: string, chars: string, vals: number[]): number {
const d: number[] = Array.from({ length: 26 }, (_, i) => i + 1);
for (let i = 0; i < chars.length; ++i) {
d[chars.charCodeAt(i) - 97] = vals[i];
}
let ans = 0;
let f = 0;
for (const c of s) {
f = Math.max(f, 0) + d[c.charCodeAt(0) - 97];
ans = Math.max(ans, f);
}
return ans;
}