尽可能使字符串相等
题目 尽可能使字符串相等
思路分析
滑动窗口裸题 右边界一直动 左边界只有不满足时移动
求得区间最大值
代码实现
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n=s.size(),cost[s.size()];
for(int i=0;i<n;i++){
cost[i]=abs(s[i]-t[i]);
}
int l=0,r=0;
int sum=0,maxlen=0;
while(r<n){
sum+=cost[r];
while(sum>maxCost){
sum-=cost[l];
l++;
}
maxlen=max(maxlen,r-l+1);
r++;
}
return maxlen;
}
};
同类题型
视频讲解
⬅️ 壁画 🏠 00-刷题理模型 ➡️ 所有奇数长度子数组的和
💬 评论