Skip to content

Commit

Permalink
Create Take K of Each Character From Left and Right
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhiupman568 authored Nov 20, 2024
1 parent 342ea3c commit 4e7f8b3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Take K of Each Character From Left and Right
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int takeCharacters(string s, int k) {

int N = s.length(),j = 0, ans = N,window = 0;
unordered_map<char,int>count;

for(auto c : s)
count[c]++;

if(count['a'] < k || count['b'] <k || count['c']<k)
return -1;

for(int i=0;i<N;i++){

count[s[i]]--;
window++;

while(count[s[i]]<k){
count[s[j++]]++;
window--;
}

ans = min(ans,N- window);
}

return ans;
}
};

0 comments on commit 4e7f8b3

Please sign in to comment.