← Home

424. Longest Repeating Character Replacement

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
424

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 424. Longest Repeating Character Replacement, the solution in this repository is mainly a two pointers solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers, sliding window.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are characterReplacement.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 16 ms, faster than 53.88% of C++ online submissions for Longest Repeating Character Replacement.
02//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Longest Repeating Character Replacement.
03
04class Solution {
05public:
06    int characterReplacement(string s, int k) {
07        int N = s.size();
08        int ans = 0;
09        
10        // for(int i = 0; i < 26; i++){
11        //     lengths[i+'A'] = 0;
12        //     used[i+'A'] = 0;
13        // }
14           
15        for(int i = 0; i < 26; i++){
16            int length = 0;
17            int used = 0;
18            int left = 0, right = 0;
19            char c = 'A' + i;
20            
21            //skip the character not in s
22            if(s.find(c) == string::npos)
23                continue;
24            // cout << c << endl;
25            
26            while(right < N){
27                if(left > 0 && s[left-1] != c){
28                    used--;
29                }
30                // cout << "[" << left << ", " << right << "), used: " << used << endl;
31                while(right < N && (s[right] == c || used < k)){
32                    if(s[right] != c){
33                        used++;
34                    }
35                    right++;
36                    // cout << "[" << left << ", " << right << "), used: " << used << endl;
37                }
38
39                length = max(length, right-left);
40                // cout << "[" << left << ", " << right << "), length: " << length << ", used: " << used << endl;
41
42                left++;
43            }
44            
45            ans = max(length, ans);
46            
47            // cout << "ans: " << ans << endl;
48        }
49        
50        return ans;
51    }
52};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.