Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int getMaxUniqueLetters(string s){
- bool map[26] = {0};
- int unique = 0;
- for(char c: s){
- if(!map[c-'a']){
- unique++;
- map[c-'a'] = true;
- }
- }
- return unique;
- }
- int longestSubstring(string s, int k) {
- int count[26] = {0};
- int maxUnique = getMaxUniqueLetters(s);
- int ans = 0;
- for(int currUnique=1; currUnique<=maxUnique; currUnique++){
- memset(count, 0, sizeof(count));
- int left=0, right=0, unique=0, countAtLeastK=0;
- while(right<s.length()){
- if(unique <= currUnique){
- int c = s[right]-'a';
- if(count[c] == 0) unique++;
- count[c]++;
- if(count[c] == k) countAtLeastK++;
- right++;
- }
- else{
- int c = s[left]-'a';
- if(count[c] == k) countAtLeastK--;
- count[c]--;
- if(count[c] == 0) unique--;
- left++;
- }
- if(unique == currUnique && countAtLeastK == currUnique)
- ans = max(ans, right-left);
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement