Ninja technique🥷 to ACE DSA Interviews.
length_of_string - max_frequency_of_a_character_in_string
length_of_string - max_frequency_of_a_character_in_string <= k
class Solution {
public:
int characterReplacement(string s, int k)
{
int n = s.size();
unordered_map<char, int> mp;
int ans = 0;
int maxFreq = 0;
int j = 0;
for (int i = 0; i < n; i++) {
mp[s[i]]++;
maxFreq = max(maxFreq, mp[s[i]]);
if (i - j - maxFreq + 1 > k) {
mp[s[j]]--;
j++;
}
ans = max(ans, i - j + 1);
}
return ans;
}
};