File tree 2 files changed +40
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -43,4 +43,33 @@ public String removeDuplicates(String s, int k) {
43
43
return sb .reverse ().toString ();
44
44
}
45
45
}
46
+
47
+ public static class Solution2 {
48
+ public String removeDuplicates (String s , int k ) {
49
+ StringBuilder sb = new StringBuilder ();
50
+ int dupCount = 0 ;
51
+ for (int i = 0 ; i < s .length (); i ++) {
52
+ if (sb .length () != 0 && sb .charAt (sb .length () - 1 ) == s .charAt (i )) {
53
+ dupCount ++;
54
+ } else {
55
+ dupCount = 1 ;
56
+ }
57
+ sb .append (s .charAt (i ));
58
+ if (dupCount == k ) {
59
+ sb .setLength (sb .length () - k );
60
+ if (i + 1 < s .length ()) {
61
+ dupCount = 0 ;
62
+ for (int j = sb .length () - 1 ; j >= 0 ; j --) {
63
+ if (sb .charAt (j ) == s .charAt (i + 1 )) {
64
+ dupCount ++;
65
+ } else {
66
+ break ;
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ return sb .toString ();
73
+ }
74
+ }
46
75
}
Original file line number Diff line number Diff line change 9
9
public class _1209Test {
10
10
11
11
private static _1209 .Solution1 solution1 ;
12
+ private static _1209 .Solution2 solution2 ;
12
13
13
14
@ BeforeClass
14
15
public static void setup () {
15
16
solution1 = new _1209 .Solution1 ();
17
+ solution2 = new _1209 .Solution2 ();
16
18
}
17
19
18
20
@ Test
19
21
public void test1 () {
20
22
assertEquals ("abcd" , solution1 .removeDuplicates ("abcd" , 2 ));
23
+ assertEquals ("abcd" , solution2 .removeDuplicates ("abcd" , 2 ));
21
24
}
22
25
23
26
@ Test
24
27
public void test2 () {
25
28
assertEquals ("aa" , solution1 .removeDuplicates ("deeedbbcccbdaa" , 3 ));
29
+ assertEquals ("aa" , solution2 .removeDuplicates ("deeedbbcccbdaa" , 3 ));
26
30
}
27
31
28
32
@ Test
29
33
public void test3 () {
30
34
assertEquals ("ps" , solution1 .removeDuplicates ("pbbcggttciiippooaais" , 2 ));
35
+ assertEquals ("ps" , solution2 .removeDuplicates ("pbbcggttciiippooaais" , 2 ));
36
+ }
37
+
38
+ @ Test
39
+ public void test4 () {
40
+ assertEquals ("ghayqgq" , solution1 .removeDuplicates ("ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq" , 7 ));
41
+ assertEquals ("ghayqgq" , solution2 .removeDuplicates ("ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq" , 7 ));
31
42
}
32
43
33
44
}
You can’t perform that action at this time.
0 commit comments