You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0400-0499/0424.Longest Repeating Character Replacement/README_EN.md
+61-44
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,15 @@ There may exists other ways to achieve this answer too.</pre>
55
55
56
56
<!-- solution:start -->
57
57
58
-
### Solution 1
58
+
### Solution 1: Two Pointers
59
+
60
+
We use a hash table `cnt` to count the occurrence of each character in the string, and two pointers `l` and `r` to maintain a sliding window, such that the size of the window minus the count of the most frequent character does not exceed $k$.
61
+
62
+
We iterate through the string, updating the right boundary `r` of the window each time, updating the count of characters within the window, and updating the maximum count `mx` of the characters that have appeared. When the size of the window minus `mx` is greater than $k$, we need to shrink the left boundary `l` of the window, updating the count of characters within the window, until the size of the window minus `mx` is no longer greater than $k$.
63
+
64
+
Finally, the answer is $n - l$, where $n$ is the length of the string.
65
+
66
+
The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string, and $|\Sigma|$ is the size of the character set. In this problem, the character set is uppercase English letters, so $|\Sigma| = 26$.
59
67
60
68
<!-- tabs:start -->
61
69
@@ -64,36 +72,32 @@ There may exists other ways to achieve this answer too.</pre>
0 commit comments