|
| 1 | +# 424. [Longest Repeating Character Replacement](<https://leetcode.com/problems/longest-repeating-character-replacement>) |
| 2 | + |
| 3 | +*[Back to top](<../README.md>)* |
| 4 | + |
| 5 | +------ |
| 6 | + |
| 7 | +> *First completed : July 05, 2024* |
| 8 | +> |
| 9 | +> *Last updated : July 05, 2024* |
| 10 | +
|
| 11 | + |
| 12 | +------ |
| 13 | + |
| 14 | +> **Related Topics** : **[Hash Table](<by_topic/Hash Table.md>), [String](<by_topic/String.md>), [Sliding Window](<by_topic/Sliding Window.md>)** |
| 15 | +> |
| 16 | +> **Acceptance Rate** : **54.193 %** |
| 17 | +
|
| 18 | + |
| 19 | +------ |
| 20 | + |
| 21 | +*To see the question prompt, click the title.* |
| 22 | + |
| 23 | +> #### Version 5 (Optimal) |
| 24 | +> ``` |
| 25 | +> The final optimization that had to be made was not updating the max frequency |
| 26 | +> downwards at any given moment. Since we're only looking at the context of our |
| 27 | +> window, the maximal window will require the most repeated characters, since all |
| 28 | +> others must be changed to match them. Therefore, if the max frequency goes down, |
| 29 | +> we know for certain that that window won't be valid, so we should just |
| 30 | +> shift over an index for both the left and right pointers. |
| 31 | +> |
| 32 | +> This brought the runtime down to around 75ms, in line with all the |
| 33 | +> submitted optimal solutions and perfectly within the bell curve. |
| 34 | +> ``` |
| 35 | +> |
| 36 | +> #### Version 4 |
| 37 | +> ``` |
| 38 | +> Here, I changed out the way I'd calculate the total number of |
| 39 | +> non-max frequency characters to a mathematical approach. I had overlooked |
| 40 | +> it in my haste initially, but the number of characters that would have to be |
| 41 | +> changed was simply the length - the frequency of the most frequent character. |
| 42 | +> |
| 43 | +> The effect was essentially unnoticable however based on how LeetCode |
| 44 | +> runs their tests and the time remained similar to Version 3. |
| 45 | +> ``` |
| 46 | +> |
| 47 | +> #### Version 3 |
| 48 | +> ``` |
| 49 | +> Compared to Version 2, the improvment here was minimal. I adjusted the |
| 50 | +> tracking variable for the longest window to start at length k, since we'd |
| 51 | +> at minimum find a case where every letter is unique, thus giving us a result |
| 52 | +> of k+1 length. |
| 53 | +> |
| 54 | +> This reduced the time down to around 180ms and the 11% region. |
| 55 | +> ``` |
| 56 | +> |
| 57 | +> #### Version 2 |
| 58 | +> ``` |
| 59 | +> In this version, I made the slight adjustment to only check windows |
| 60 | +> that were larger than the previous window size. This was a step |
| 61 | +> in the right direction, and was based on a similar idea as the optimal |
| 62 | +> solution as it turns out. |
| 63 | +> |
| 64 | +> This small adjustment resulted in a drop to 320ms per run. |
| 65 | +> ``` |
| 66 | +> |
| 67 | +> #### Version 1 |
| 68 | +> ``` |
| 69 | +> This was my first attempt at the question which passed without |
| 70 | +> any TLE issues first try in under 9 minutes. However, it was on |
| 71 | +> the brink of a TLE. On average, the runtime was measured at the |
| 72 | +> bottom 5%, taking 520ms approx. The idea was correct, |
| 73 | +> but it did need some modifications. |
| 74 | +> ``` |
| 75 | +
|
| 76 | +------ |
| 77 | +
|
| 78 | +## Solutions |
| 79 | +
|
| 80 | +- [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>) |
| 81 | +- [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>) |
| 82 | +- [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>) |
| 83 | +- [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>) |
| 84 | +- [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>) |
| 85 | +### Python |
| 86 | +#### [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>) |
| 87 | +```Python |
| 88 | +class Solution: |
| 89 | + def characterReplacement(self, s: str, k: int) -> int: |
| 90 | + cnt = Counter(s[0]) |
| 91 | +
|
| 92 | + longest = 0 |
| 93 | + left, right = 0, 0 |
| 94 | + while right < len(s) : |
| 95 | + maxxChar = max(cnt, key=lambda x: cnt[x]) |
| 96 | + sumOthers = sum([cnt[x] for x in cnt if x != maxxChar]) |
| 97 | +
|
| 98 | + if sumOthers > k : |
| 99 | + cnt[s[left]] -= 1 |
| 100 | + left += 1 |
| 101 | +
|
| 102 | + else : |
| 103 | + longest = max(longest, sumOthers + cnt[maxxChar]) |
| 104 | + right += 1 |
| 105 | + if right < len(s) : |
| 106 | + cnt[s[right]] += 1 |
| 107 | +
|
| 108 | + return longest |
| 109 | +``` |
| 110 | +
|
| 111 | +#### [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>) |
| 112 | +```Python |
| 113 | +class Solution: |
| 114 | + def characterReplacement(self, s: str, k: int) -> int: |
| 115 | + cnt = Counter(s[0]) |
| 116 | + |
| 117 | + longest = 0 |
| 118 | + left, right = 0, 0 |
| 119 | + while right < len(s) : |
| 120 | + if right - left + 1 <= longest : |
| 121 | + right += 1 |
| 122 | + if right < len(s) : |
| 123 | + cnt[s[right]] += 1 |
| 124 | + continue |
| 125 | + |
| 126 | + maxxChar = max(cnt, key=lambda x: cnt[x]) |
| 127 | + sumOthers = sum([cnt[x] for x in cnt if x != maxxChar]) |
| 128 | + |
| 129 | + if sumOthers > k : |
| 130 | + cnt[s[left]] -= 1 |
| 131 | + left += 1 |
| 132 | + |
| 133 | + else : |
| 134 | + longest = right - left + 1 |
| 135 | + right += 1 |
| 136 | + if right < len(s) : |
| 137 | + cnt[s[right]] += 1 |
| 138 | + |
| 139 | + return longest |
| 140 | +``` |
| 141 | + |
| 142 | +#### [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>) |
| 143 | +```Python |
| 144 | +class Solution: |
| 145 | + def characterReplacement(self, s: str, k: int) -> int: |
| 146 | + cnt = Counter(s[0]) |
| 147 | + |
| 148 | + longest = min(k, len(s)) |
| 149 | + left, right = 0, 0 |
| 150 | + while right < len(s) : |
| 151 | + if right - left + 1 <= longest : |
| 152 | + right += 1 |
| 153 | + if right < len(s) : |
| 154 | + cnt[s[right]] += 1 |
| 155 | + continue |
| 156 | + |
| 157 | + maxxChar = max(cnt, key=lambda x: cnt[x]) |
| 158 | + sumOthers = sum([cnt[x] for x in cnt if x != maxxChar]) |
| 159 | + |
| 160 | + if sumOthers > k : |
| 161 | + cnt[s[left]] -= 1 |
| 162 | + left += 1 |
| 163 | + |
| 164 | + else : |
| 165 | + longest = right - left + 1 |
| 166 | + right += 1 |
| 167 | + if right < len(s) : |
| 168 | + cnt[s[right]] += 1 |
| 169 | + |
| 170 | + return longest |
| 171 | +``` |
| 172 | + |
| 173 | +#### [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>) |
| 174 | +```Python |
| 175 | +class Solution: |
| 176 | + def characterReplacement(self, s: str, k: int) -> int: |
| 177 | + cnt = Counter(s[0]) |
| 178 | + |
| 179 | + longest = min(k, len(s)) |
| 180 | + left, right = 0, 0 |
| 181 | + while right < len(s) : |
| 182 | + if right - left + 1 <= longest : |
| 183 | + right += 1 |
| 184 | + if right < len(s) : |
| 185 | + cnt[s[right]] += 1 |
| 186 | + continue |
| 187 | + |
| 188 | + maxxChar = max(cnt, key=lambda x: cnt[x]) |
| 189 | + sumOthers = right - left + 1 - cnt[maxxChar] |
| 190 | + |
| 191 | + if sumOthers > k : |
| 192 | + cnt[s[left]] -= 1 |
| 193 | + left += 1 |
| 194 | + |
| 195 | + else : |
| 196 | + longest = right - left + 1 |
| 197 | + right += 1 |
| 198 | + if right < len(s) : |
| 199 | + cnt[s[right]] += 1 |
| 200 | + |
| 201 | + return longest |
| 202 | +``` |
| 203 | + |
| 204 | +#### [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>) |
| 205 | +```Python |
| 206 | +class Solution: |
| 207 | + def characterReplacement(self, s: str, k: int) -> int: |
| 208 | + cnt = Counter() |
| 209 | + |
| 210 | + left, right = 0, 0 |
| 211 | + maxFreq = 0 |
| 212 | + for right in range(len(s)) : |
| 213 | + cnt[s[right]] += 1 |
| 214 | + maxFreq = max(maxFreq, cnt[s[right]]) |
| 215 | + |
| 216 | + if right - left + 1 - maxFreq > k : |
| 217 | + cnt[s[left]] -= 1 |
| 218 | + left += 1 |
| 219 | + |
| 220 | + return right - left + 1 |
| 221 | +``` |
| 222 | + |
0 commit comments