forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_424.java
69 lines (64 loc) · 2.17 KB
/
_424.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _424 {
public static class Solution1 {
//credit: https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation
public int characterReplacement(String s, int k) {
int len = s.length();
int[] count = new int[26];
int start = 0;
int maxCount = 0;
int maxLength = 0;
for (int end = 0; end < len; end++) {
maxCount = Math.max(maxCount, ++count[s.charAt(end) - 'A']);
while (end - start + 1 - maxCount > k) {
count[s.charAt(start) - 'A']--;
start++;
}
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
}
public static class Solution2 {
/**
* My original solution using Sliding Window technique:
* I try to use each character as the possible candidate to find all solutions and compare.
*/
public int characterReplacement(String s, int k) {
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray()) {
set.add(c);
}
int ans = 0;
for (char c : set) {
ans = Math.max(ans, slidingWindow(c, s, k));
}
return ans;
}
private int slidingWindow(char c, String s, int k) {
int left = 0;
int right = 0;
int ans = 0;
while (right < s.length()) {
if (s.charAt(right) != c) {
if (k > 0) {
k--;
right++;
} else {
while (left < s.length() && s.charAt(left) == c) {
left++;
}
left++;
k++;
}
} else {
right++;
}
ans = Math.max(ans, right - left);
}
return ans;
}
}
}