File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1+ function characterReplacement ( s : string , k : number ) : number {
2+ let longestLength = 0 ;
3+ let windowStart = 0 ;
4+
5+ const countMap = new Map < string , number > ( ) ;
6+ let maxCount = 0 ;
7+
8+ for ( let windowEnd = 0 ; windowEnd < s . length ; windowEnd ++ ) {
9+ const currentCh = s [ windowEnd ] ;
10+ countMap . set ( currentCh , ( countMap . get ( currentCh ) || 0 ) + 1 ) ;
11+
12+ maxCount = Math . max ( maxCount , countMap . get ( currentCh ) || 0 ) ;
13+ if ( windowEnd - windowStart + 1 - maxCount > k ) {
14+ countMap . set ( s [ windowStart ] , ( countMap . get ( s [ windowStart ] ) || 0 ) - 1 ) ;
15+
16+ windowStart ++ ;
17+ }
18+ longestLength = Math . max ( longestLength , windowEnd - windowStart + 1 ) ;
19+ }
20+
21+ return longestLength ;
22+ }
You can’t perform that action at this time.
0 commit comments