0%

至少有K个重复字符的最长子串

思路讲解

https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/description/

其实就是双指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void cal(ll uni){
vll cnt(28);
ll l=0,r=0;
ll cur=0,bigk=0;
while (r<SZ(s)) {
if(cur<=uni){
int idx=s[r]-'a';
if(cnt[idx]==0) ++cur;
cnt[idx]++;
if(cnt[idx]==K) ++bigk;
++r;
}else{
int idx=s[l]-'a';
if(cnt[idx]==K) --bigk;
--cnt[idx];
if(cnt[idx]==0) --cur;
++l;
}
if(cur==uni && bigk==cur){
ans=max(ans,r-l);
}
}
}

AC代码

AC

https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/submissions/658772401/

心路历程(WA,TLE,MLE……)