C++_AC_Two Pointers


  • 0
    class Solution {
    public:
    int characterReplacement(string s, int k) {
        if(s.empty()) return 0;
        int res = INT_MIN;
        for(int cnt = 0; cnt < 26; ++cnt){
            char curChar = 'A' + cnt;
            int i = 0, j = 0, r = k;
            while(j < s.size()){
                while(j < s.size()){
                    if(s[j] != curChar){
                        if(r == 0) break;
                        else{r--;}
                    }
                    j++;
                }
                res = max(res, j - i);
                while(s[i] == curChar){i++;}
                ++i;//skip to the next non-curChar position.
                r++;
            }
        }
        return res;
    }
    };

Log in to reply
 

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