75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

424. Longest Repeating Character Replacement (Medium)

Sliding Window

Code

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;
    }
};