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