class Solution {
public:
int takeCharacters(string s, int k) {
vector<int>need(3,-k);
vector<int>window(3,0);
for(char c:s)
need[c-'a']++;
for(int i=0;i<3;++i)
if(need[i]<0) return -1;
int left=0,right=0,unvalid=0,n=s.length();
int res=0;
while(right<n){
char c=s[right];
right++;
window[c-'a']++;
if(window[c-'a']>need[c-'a'])
unvalid++;
while(unvalid>0){
char d = s[left];
left++;
window[d-'a']--;
if(window[d-'a']==need[d-'a'])
unvalid--;
}
res = max(res,right-left);
}
return n-res;
}
};
2516. 每种字符至少取 K 个
最新推荐文章于 2024-02-11 20:11:40 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)