Skip to content

Commit f5d5801

Browse files
committed
docs: add Longest Repeating Character Replacement
1 parent 0c9f7b3 commit f5d5801

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ https://neetcode.io/roadmap
3535
| 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | Medium | [TypeScript](./TypeScript/981.time-based-key-value-store.ts) | Binary Search |
3636
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | Easy | [TypeScript](./TypeScript/121.best-time-to-buy-and-sell-stock.ts) | Sliding Window |
3737
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | Medium | [TypeScript](./TypeScript/3.longest-substring-without-repeating-characters.ts) | Sliding Window |
38+
| 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | Medium | [TypeScript](./TypeScript/424.longest-repeating-character-replacement.ts) | Sliding Window |
3839

3940
### Others
4041

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function characterReplacement(s: string, k: number): number {
2+
const map = new Map<string, number>();
3+
let left = 0;
4+
let right = 0;
5+
let result = 0;
6+
7+
while (right < s.length) {
8+
map.set(s[right], (map.get(s[right]) ?? 0) + 1);
9+
while (right - left + 1 - Math.max(...map.values()) > k) {
10+
map.set(s[left], map.get(s[left])! - 1);
11+
left++;
12+
}
13+
result = Math.max(result, right - left + 1);
14+
right++;
15+
}
16+
17+
return result;
18+
}
19+
20+
function characterReplacement2(s: string, k: number): number {
21+
const map = new Map<string, number>();
22+
let left = 0;
23+
let right = 0;
24+
let result = 0;
25+
let maxf = 0;
26+
27+
while (right < s.length) {
28+
map.set(s[right], (map.get(s[right]) ?? 0) + 1);
29+
maxf = Math.max(maxf, map.get(s[right])!);
30+
while (right - left + 1 - maxf > k) {
31+
map.set(s[left], map.get(s[left])! - 1);
32+
left++;
33+
}
34+
result = Math.max(result, right - left + 1);
35+
right++;
36+
}
37+
38+
return result;
39+
}

0 commit comments

Comments
 (0)