Skip to content

Commit bbd06a0

Browse files
committed
Add solution #424
1 parent ee5d758 commit bbd06a0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@
338338
420|[Strong Password Checker](./0420-strong-password-checker.js)|Hard|
339339
421|[Maximum XOR of Two Numbers in an Array](./0421-maximum-xor-of-two-numbers-in-an-array.js)|Medium|
340340
423|[Reconstruct Original Digits from English](./0423-reconstruct-original-digits-from-english.js)|Medium|
341+
424|[Longest Repeating Character Replacement](./0424-longest-repeating-character-replacement.js)|Medium|
341342
434|[Number of Segments in a String](./0434-number-of-segments-in-a-string.js)|Easy|
342343
435|[Non-overlapping Intervals](./0435-non-overlapping-intervals.js)|Medium|
343344
437|[Path Sum III](./0437-path-sum-iii.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 424. Longest Repeating Character Replacement
3+
* https://leetcode.com/problems/longest-repeating-character-replacement/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s and an integer k. You can choose any character of the string
7+
* and change it to any other uppercase English character. You can perform this operation
8+
* at most k times.
9+
*
10+
* Return the length of the longest substring containing the same letter you can get after
11+
* performing the above operations.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var characterReplacement = function(s, k) {
20+
const count = new Map();
21+
let max = 0;
22+
let left = 0;
23+
24+
return s.split('').reduce((maxLength, char, right) => {
25+
count.set(char, (count.get(char) || 0) + 1);
26+
max = Math.max(max, count.get(char));
27+
if (right - left + 1 - max > k) {
28+
count.set(s[left], count.get(s[left++]) - 1);
29+
}
30+
return Math.max(maxLength, right - left + 1);
31+
}, 0);
32+
};

0 commit comments

Comments
 (0)