File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๋ฌธ์์ด s์ ์ ์ k๊ฐ ์ฃผ์ด์ง๋ค.
3+ * ๋ฌธ์์ด ๋ด ์๋ฌด ๋ฌธ์๋ ๋๋ฌธ์ ์๋ฌธ์๋ก ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ ํด๋น ์ฐ์ฐ์ k๋ฒ๊น์ง ํ ์ ์์ ๋,
4+ * ๊ฐ์ ๋ฌธ์๋ฅผ ํฌํจํ๋ ๊ฐ์ฅ ๊ธด ๋ถ๋ถ ์์ด์ ๋ฐํํ์ธ์.
5+ */
6+ class Solution {
7+
8+ // ์๊ฐ๋ณต์ก๋: O(n)
9+ public int characterReplacement (String s , int k ) {
10+
11+ int [] chars = new int [26 ];
12+ int left = 0 ;
13+ int maxLength = 0 ;
14+ int maxCnt = 0 ;
15+
16+ for (int right = 0 ; right < s .length (); right ++) {
17+ int charIdx = s .charAt (right ) - 'A' ;
18+ chars [charIdx ]++;
19+ // ํ์ฌ ์๋์ฐ ๋ด์์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ๋ ๋ฌธ์์ ๊ฐ์
20+ maxCnt = Math .max (chars [charIdx ], maxCnt );
21+
22+ // window size - max cnt > k (window ์ฌ์ด์ฆ ์กฐ์ )
23+ while (right - left + 1 - maxCnt > k ) {
24+ chars [s .charAt (left ) - 'A' ]--;
25+ left ++;
26+ }
27+
28+ maxLength = Math .max (maxLength , right - left + 1 );
29+ }
30+
31+ return maxLength ;
32+
33+ }
34+ }
35+
You canโt perform that action at this time.
0 commit comments