Leetcode—159. 至多包含两个不同字符的最长子串【中等】Plus
2025每日刷题(208)
Leetcode—159. 至多包含两个不同字符的最长子串
实现代码
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> cnt(128);
int len = s.length();
int ans = 0;
int dif = 0;
for(int i = 0, j = 0; j < len; j++) {
if(++cnt[s[j]] == 1) {
dif++;
}
while(i < len && dif == 3) {
if(--cnt[s[i++]] == 0) {
dif--;
}
}
ans = max(ans, j - i + 1);
}
return ans;
}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!