Skip to content

Commit aed3eab

Browse files
add a solution for 424
1 parent 3f36ab4 commit aed3eab

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

src/main/java/com/fishercoder/solutions/_424.java

+45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.HashSet;
4+
import java.util.Set;
5+
36
public class _424 {
47

58
public static class Solution1 {
@@ -21,4 +24,46 @@ public int characterReplacement(String s, int k) {
2124
return maxLength;
2225
}
2326
}
27+
28+
public static class Solution2 {
29+
/**
30+
* My original solution using Sliding Window technique:
31+
* I try to use each character as the possible candidate to find all solutions and compare.
32+
*/
33+
public int characterReplacement(String s, int k) {
34+
Set<Character> set = new HashSet<>();
35+
for (char c : s.toCharArray()) {
36+
set.add(c);
37+
}
38+
int ans = 0;
39+
for (char c : set) {
40+
ans = Math.max(ans, slidingWindow(c, s, k));
41+
}
42+
return ans;
43+
}
44+
45+
private int slidingWindow(char c, String s, int k) {
46+
int left = 0;
47+
int right = 0;
48+
int ans = 0;
49+
while (right < s.length()) {
50+
if (s.charAt(right) != c) {
51+
if (k > 0) {
52+
k--;
53+
right++;
54+
} else {
55+
while (left < s.length() && s.charAt(left) == c) {
56+
left++;
57+
}
58+
left++;
59+
k++;
60+
}
61+
} else {
62+
right++;
63+
}
64+
ans = Math.max(ans, right - left);
65+
}
66+
return ans;
67+
}
68+
}
2469
}

src/test/java/com/fishercoder/_424Test.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,63 @@
88

99
public class _424Test {
1010
private static _424.Solution1 solution1;
11+
private static _424.Solution2 solution2;
1112
private static String s;
1213
private static int k;
13-
private static int actual;
1414
private static int expected;
1515

1616
@BeforeClass
1717
public static void setup() {
1818
solution1 = new _424.Solution1();
19+
solution2 = new _424.Solution2();
1920
}
2021

2122
@Test
2223
public void test1() {
2324
s = "ABAB";
2425
k = 2;
25-
actual = solution1.characterReplacement(s, k);
2626
expected = 4;
27-
assertEquals(expected, actual);
27+
assertEquals(expected, solution1.characterReplacement(s, k));
28+
assertEquals(expected, solution2.characterReplacement(s, k));
2829
}
2930

3031
@Test
3132
public void test2() {
3233
s = "AABABBA";
3334
k = 1;
34-
actual = solution1.characterReplacement(s, k);
3535
expected = 4;
36-
assertEquals(expected, actual);
36+
assertEquals(expected, solution1.characterReplacement(s, k));
3737
}
3838

3939
@Test
4040
public void test3() {
4141
s = "AAAA";
4242
k = 2;
43-
actual = solution1.characterReplacement(s, k);
4443
expected = 4;
45-
assertEquals(expected, actual);
44+
assertEquals(expected, solution1.characterReplacement(s, k));
4645
}
4746

4847
@Test
4948
public void test4() {
5049
s = "AAAB";
5150
k = 0;
52-
actual = solution1.characterReplacement(s, k);
5351
expected = 3;
54-
assertEquals(expected, actual);
52+
assertEquals(expected, solution1.characterReplacement(s, k));
5553
}
5654

5755
@Test
5856
public void test5() {
5957
s = "AABA";
6058
k = 0;
61-
actual = solution1.characterReplacement(s, k);
6259
expected = 2;
63-
assertEquals(expected, actual);
60+
assertEquals(expected, solution1.characterReplacement(s, k));
6461
}
6562

6663
@Test
6764
public void test6() {
6865
s = "ABBB";
6966
k = 2;
70-
actual = solution1.characterReplacement(s, k);
7167
expected = 4;
72-
assertEquals(expected, actual);
68+
assertEquals(expected, solution1.characterReplacement(s, k));
7369
}
7470
}

0 commit comments

Comments
 (0)