File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 338
338
420|[ Strong Password Checker] ( ./0420-strong-password-checker.js ) |Hard|
339
339
421|[ Maximum XOR of Two Numbers in an Array] ( ./0421-maximum-xor-of-two-numbers-in-an-array.js ) |Medium|
340
340
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|
341
342
434|[ Number of Segments in a String] ( ./0434-number-of-segments-in-a-string.js ) |Easy|
342
343
435|[ Non-overlapping Intervals] ( ./0435-non-overlapping-intervals.js ) |Medium|
343
344
437|[ Path Sum III] ( ./0437-path-sum-iii.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments