Skip to content

Commit 86cd045

Browse files
refactor 424
1 parent 7ee8d0d commit 86cd045

File tree

2 files changed

+24
-83
lines changed

2 files changed

+24
-83
lines changed

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

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -34,79 +34,23 @@
3434
*/
3535
public class _424 {
3636

37-
//credit: https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation
38-
public int characterReplacement(String s, int k) {
39-
int len = s.length();
40-
int[] count = new int[26];
41-
int start = 0;
42-
int maxCount = 0;
43-
int maxLength = 0;
44-
for (int end = 0; end < len; end++) {
45-
maxCount = Math.max(maxCount, ++count[s.charAt(end) - 'A']);
46-
while (end - start + 1 - maxCount > k) {
47-
count[s.charAt(start) - 'A']--;
48-
start++;
49-
}
50-
maxLength = Math.max(maxLength, end - start + 1);
51-
}
52-
return maxLength;
53-
}
54-
55-
//this one can pass all test from test1 to test5, but tests like test6 won't pass
56-
public int characterReplacement_failed_attempt(String s, int k) {
57-
int longest = 0;
58-
if (s == null || s.length() == 0) {
59-
return 0;
60-
}
61-
for (int i = 0; i < s.length(); i++) {
62-
int count = 1;
63-
char val = s.charAt(i);
64-
for (int j = i + 1; j < s.length(); j++) {
65-
if (s.charAt(j) == val) {
66-
count++;
67-
} else {
68-
if (k > 0) {
69-
count++;
70-
char replace = s.charAt(j);
71-
int kCounter = k - 1;
72-
for (int p = j + 1; p < s.length(); p++) {
73-
if (kCounter <= 0) {
74-
if (val == s.charAt(p)) {
75-
count++;
76-
} else {
77-
longest = Math.max(longest, count);
78-
break;
79-
}
80-
} else {
81-
if (val == s.charAt(p)) {
82-
count++;
83-
} else if (replace == s.charAt(p)) {
84-
count++;
85-
kCounter--;
86-
} else {
87-
longest = Math.max(longest, count);
88-
break;
89-
}
90-
}
91-
}
92-
if (kCounter <= 0) {
93-
longest = Math.max(longest, count);
94-
break;
95-
}
96-
} else {
97-
if (s.charAt(j) == val) {
98-
count++;
99-
} else {
100-
longest = Math.max(longest, count);
101-
break;
102-
}
103-
}
37+
public static class Solution1 {
38+
//credit: https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation
39+
public int characterReplacement(String s, int k) {
40+
int len = s.length();
41+
int[] count = new int[26];
42+
int start = 0;
43+
int maxCount = 0;
44+
int maxLength = 0;
45+
for (int end = 0; end < len; end++) {
46+
maxCount = Math.max(maxCount, ++count[s.charAt(end) - 'A']);
47+
while (end - start + 1 - maxCount > k) {
48+
count[s.charAt(start) - 'A']--;
49+
start++;
10450
}
105-
longest = Math.max(longest, count);
51+
maxLength = Math.max(maxLength, end - start + 1);
10652
}
107-
longest = Math.max(longest, count);
53+
return maxLength;
10854
}
109-
return longest;
11055
}
111-
112-
}
56+
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/7/17.
11-
*/
129
public class _424Test {
13-
private static _424 test;
10+
private static _424.Solution1 solution1;
1411
private static String s;
1512
private static int k;
1613
private static int actual;
1714
private static int expected;
1815

1916
@BeforeClass
2017
public static void setup() {
21-
test = new _424();
18+
solution1 = new _424.Solution1();
2219
}
2320

2421
@Test
2522
public void test1() {
2623
s = "ABAB";
2724
k = 2;
28-
actual = test.characterReplacement(s, k);
25+
actual = solution1.characterReplacement(s, k);
2926
expected = 4;
3027
assertEquals(expected, actual);
3128
}
@@ -34,7 +31,7 @@ public void test1() {
3431
public void test2() {
3532
s = "AABABBA";
3633
k = 1;
37-
actual = test.characterReplacement(s, k);
34+
actual = solution1.characterReplacement(s, k);
3835
expected = 4;
3936
assertEquals(expected, actual);
4037
}
@@ -43,7 +40,7 @@ public void test2() {
4340
public void test3() {
4441
s = "AAAA";
4542
k = 2;
46-
actual = test.characterReplacement(s, k);
43+
actual = solution1.characterReplacement(s, k);
4744
expected = 4;
4845
assertEquals(expected, actual);
4946
}
@@ -52,7 +49,7 @@ public void test3() {
5249
public void test4() {
5350
s = "AAAB";
5451
k = 0;
55-
actual = test.characterReplacement(s, k);
52+
actual = solution1.characterReplacement(s, k);
5653
expected = 3;
5754
assertEquals(expected, actual);
5855
}
@@ -61,7 +58,7 @@ public void test4() {
6158
public void test5() {
6259
s = "AABA";
6360
k = 0;
64-
actual = test.characterReplacement(s, k);
61+
actual = solution1.characterReplacement(s, k);
6562
expected = 2;
6663
assertEquals(expected, actual);
6764
}
@@ -70,7 +67,7 @@ public void test5() {
7067
public void test6() {
7168
s = "ABBB";
7269
k = 2;
73-
actual = test.characterReplacement(s, k);
70+
actual = solution1.characterReplacement(s, k);
7471
expected = 4;
7572
assertEquals(expected, actual);
7673
}

0 commit comments

Comments
 (0)