Skip to content

Commit d57871a

Browse files
committed
longest repeating character replacement
1 parent 9b99564 commit d57871a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

โ€Žlongest-repeating-character-replacement/eunhwa99.javaโ€Ž

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
ย (0)