2272. 最大波动的子字符串
题目链接:2272. 最大波动的子字符串
代码如下:
//参考链接:https://leetcode.cn/problems/substring-with-largest-variance/solutions/1494890/by-endlesscheng-5775
class Solution {
public:
int largestVariance(string s) {
int res = 0;
for (char a = 'a';a <= 'z';a++) {
for (char b = 'a';b <= 'z';b++) {
if (b == a) {
continue;
}
int f0 = 0, f1 = INT_MIN;
for (char ch : s) {
if (ch == a) {
f0 = max(f0, 0) + 1;
f1++;
}
else if (ch == b) {
f1 = f0 = max(f0, 0) - 1;
}// else f0 = max(f0, 0); 可以留到 ch 等于 a 或者 b 的时候计算,f1 不变
res = max(res, f1);
}
}
}
return res;
}
};