C++ two pointer solution


  • 0
    J
    class Solution {
    public:
        int characterReplacement(string s, int k) {
            vector<int>count(26);
            int i = 0,j=0;
            int n = s.size();
            if(n==0) return 0;
            int length = INT_MIN;
            while(j<n)
            {
                count[s[j]-'A']++;
                 
                while(j-i+1-max_repeating(count)>k)
                {
                    count[s[i]-'A']--;
                    i++;
                }
                length = max(length,j-i+1);
                j++;
            }
            return length;
        }
        
        int max_repeating(vector<int>count)
        {
            int res = 0;
            for(int i= 0;i<count.size();i++)
                res = max(res,count[i]);
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.