Skip to content

Commit 700dca6

Browse files
committed
feat: longest-repeating-character-replacement
1 parent 8476fc0 commit 700dca6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-repeating-character-replacement/">week8-2.longest-repeating-character-replacement</a>
3+
* <li>Description: Return the length of the longest substring containing the same letter you can get after changing the character to any other uppercase English character</li>
4+
* <li>Topics: Hash Table, String, Sliding Window</li>
5+
* <li>Time Complexity: O(K), Runtime 7ms </li>
6+
* <li>Space Complexity: O(1), Memory 44.85MB </li>
7+
*/
8+
9+
class Solution {
10+
public int characterReplacement(String s, int k) {
11+
int left = 0;
12+
int maxCount = 0;
13+
int[] count = new int[26];
14+
int maxLength = 0;
15+
16+
for (int right = 0; right < s.length(); right++) {
17+
char c = s.charAt(right);
18+
count[c - 'A']++;
19+
maxCount = Math.max(maxCount, count[c - 'A']);
20+
21+
while (right - left + 1 > maxCount + k) {
22+
count[s.charAt(left) - 'A']--;
23+
left++;
24+
}
25+
maxLength = Math.max(maxLength, right - left + 1);
26+
}
27+
28+
return maxLength;
29+
}
30+
}

0 commit comments

Comments
 (0)