File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * @param {string } s
4+ * @param {number } k
5+ * @return {number }
6+ */
7+ var characterReplacement = function ( s , k ) {
8+ let start = 0 ;
9+ let maxLength = 0 ;
10+ let maxCharCount = 0 ;
11+ let count = { } ;
12+
13+ for ( let end = 0 ; end < s . length ; end ++ ) {
14+ const char = s [ end ] ;
15+ count [ char ] = ( count [ char ] || 0 ) + 1 ;
16+
17+ // ํ์ฌ ์๋์ฐ ์์์ ๊ฐ์ฅ ๋ง์ด ๋์จ ๋ฌธ์ ์ ๊ฐฑ์
18+ maxCharCount = Math . max ( maxCharCount , count [ char ] ) ;
19+
20+ // ๋ฐ๊ฟ์ผ ํ๋ ๋ฌธ์ ์๊ฐ k๋ณด๋ค ๋ง์ผ๋ฉด โ ์ผ์ชฝ ํฌ์ธํฐ ์ด๋
21+ if ( end - start + 1 - maxCharCount > k ) {
22+ count [ s [ start ] ] -- ;
23+ start ++ ;
24+ }
25+
26+ // ํ์ฌ ์๋์ฐ ๊ธธ์ด๋ก ์ต๋ ๊ธธ์ด ๊ฐฑ์
27+ maxLength = Math . max ( maxLength , end - start + 1 ) ;
28+ }
29+
30+ return maxLength ;
31+ } ;
You canโt perform that action at this time.
0 commit comments