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