Skip to content

Commit

Permalink
Create 2516_Take_K_of_Each_Character_From_Left_and_Right.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-avil authored Nov 20, 2024
1 parent 799b7d4 commit 13dbc0e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 2516_Take_K_of_Each_Character_From_Left_and_Right.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ███████╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
// ██╔════╝ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██║
// ███████╗ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██████╔╝ ██████╔╝ ███████║
// ╚════██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██╔═██╗ ██╔══██╗ ██╔══██║
// ███████║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ██╗ ██████╔╝ ██║ ██║
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
auto init = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
int takeCharacters(string s, int k) {
if(k==0){
return 0;
}
int aa[3]={0};
for(char ch : s){
aa[ch - 'a']++;
}
if(aa[0] < k || aa[1] < k || aa[2] < k){
return -1;
}
int i=0, j=0, res=0;
while(j<s.size()){
aa[s[j] - 'a']--;
while(i<=j && (aa[0] < k || aa[1] < k || aa[2] < k)){
aa[s[i] - 'a']++;
i++;
}
res = max(res, j-i+1);
j++;
}
return (s.size() - res);
}
};

0 comments on commit 13dbc0e

Please sign in to comment.