File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -5,13 +5,15 @@ public int characterReplacement(String s, int k) {
55 int left = 0 ;
66 int result = 0 ;
77
8- for (int right = 0 ; right < s .length (); right ++) {
8+ for (int right = 0 ; right < s .length (); right ++) { // sliding window์์์ right pointer
99 char currentChar = s .charAt (right );
10- count [currentChar - 'A' ]++;
10+ count [currentChar - 'A' ]++; // ํ์ฌ ๋ฌธ์ ์นด์ดํธ ์ฆ๊ฐ
1111 maxCount = Math .max (maxCount , count [currentChar - 'A' ]);
1212
13- while (right - left + 1 - maxCount > k ) {
14- count [s .charAt (left ) - 'A' ]--;
13+ while (right - left + 1 - maxCount > k ) { // ํ์ฌ window์ ๊ธธ์ด - ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์ ๊ฐ์ > k
14+ // ์๋์ฐ ํฌ๊ธฐ - maxCount > k๊ฐ ๋๋ฉด, k๋ฒ์ ๋ณ๊ฒฝ์ผ๋ก๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ๋์ผํ๊ฒ ๋ง๋ค ์ ์๋ค๋ ๋ป์ด๋ฏ๋ก
15+ // ์๋์ฐ์ ์ผ์ชฝ ํฌ์ธํฐ(left)๋ฅผ ์ฆ๊ฐ์์ผ ์๋์ฐ ํฌ๊ธฐ๋ฅผ ์ค์ธ๋ค.
16+ count [s .charAt (left ) - 'A' ]--; // left pointer์ ๋ฌธ์ ์นด์ดํธ ๊ฐ์
1517 left ++;
1618 }
1719
@@ -21,4 +23,6 @@ public int characterReplacement(String s, int k) {
2123
2224 return result ;
2325 }
24- }
26+ }
27+ // Time Complexity: O(n)
28+ // Space Complexity: O(1)
You canโt perform that action at this time.
0 commit comments