leetcode 1208.尽可能使字符串相等
题干
给你两个长度相同的字符串,s 和 t。
将 s 中的第 i 个字符变到 t 中的第 i 个字符需要 |s[i] - t[i]| 的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。
用于变更字符串的最大预算是 maxCost。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
如果你可以将 s 的子字符串转化为它在 t 中对应的子字符串,则返回可以转化的最大长度。
如果 s 中没有子字符串可以转化成 t 中对应的子字符串,则返回 0。
示例 1:
输入:s = “abcd”, t = “bcdf”, cost = 3
输出:3
解释:s 中的 “abc” 可以变为 “bcd”。开销为 3,所以最大长度为 3。
示例 2:
输入:s = “abcd”, t = “cdef”, cost = 3
输出:1
解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1。
示例 3:
输入:s = “abcd”, t = “acde”, cost = 0
输出:1
解释:你无法作出任何改动,所以最大长度为 1。
提示:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s 和 t 都只含小写英文字母。
题解
看到题目啊,上来就是一手经典前缀和加滑窗,由于字符串变换是一一对应的,所以用前缀和保存每一位之间的变动代价然后滑窗即可。
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
int ans = 0;
int leftBound = 0 ,rightBound = 1;
int preSum[n+5];
preSum[0] = 0;
for(int i = 1 ; i <= n ; ++i){
preSum[i] = preSum[i-1] + abs(t[i-1] - s[i-1]);
}
for(int i = 0 ; i <= n ; ++i){
cout<<preSum[i]<<' ';
}
while(rightBound <= n){
int currentCost = preSum[rightBound] - preSum[leftBound];
if(currentCost <= maxCost){
ans = max(rightBound-leftBound,ans);
}else{
leftBound++;
}
rightBound++;
}
return ans;
}
};
当然能用滑窗的话根本不写前缀和也可以。
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
int ans = 0;
int leftBound = 0 ,rightBound = 0;
int currentCost = 0;
while(rightBound < n){
currentCost += abs(t[rightBound] - s[rightBound]);
if(currentCost <= maxCost){
ans = max(rightBound-leftBound+1,ans);
}else{
currentCost -= abs(t[leftBound] - s[leftBound]);
leftBound++;
}
rightBound++;
}
return ans;
}
};