Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3bc0e2

Browse files
committedJan 7, 2022
add a solution for 1209
1 parent 79deb72 commit f3bc0e2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
 

‎src/main/java/com/fishercoder/solutions/_1209.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Deque;
4+
import java.util.LinkedList;
35
import java.util.Stack;
46

57
public class _1209 {
@@ -72,4 +74,45 @@ public String removeDuplicates(String s, int k) {
7274
return sb.toString();
7375
}
7476
}
77+
78+
public static class Solution3 {
79+
/**
80+
* My completely original solution on 1/6/2021.
81+
*/
82+
class CharCount {
83+
char c;
84+
int count;
85+
86+
public CharCount(char c, int count) {
87+
this.c = c;
88+
this.count = count;
89+
}
90+
}
91+
92+
public String removeDuplicates(String s, int k) {
93+
Deque<CharCount> stack = new LinkedList<>();
94+
for (char c : s.toCharArray()) {
95+
if (stack.isEmpty()) {
96+
stack.addLast(new CharCount(c, 1));
97+
} else {
98+
if (stack.peekLast().c == c && stack.peekLast().count + 1 == k) {
99+
stack.pollLast();
100+
} else if (stack.peekLast().c == c) {
101+
stack.addLast(new CharCount(c, stack.pollLast().count + 1));
102+
} else {
103+
stack.addLast(new CharCount(c, 1));
104+
}
105+
}
106+
}
107+
StringBuilder sb = new StringBuilder();
108+
while (!stack.isEmpty()) {
109+
CharCount pair = stack.pollLast();
110+
int count = pair.count;
111+
while (count-- > 0) {
112+
sb.append(pair.c);
113+
}
114+
}
115+
return sb.reverse().toString();
116+
}
117+
}
75118
}

‎src/test/java/com/fishercoder/_1209Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,41 @@ public class _1209Test {
1010

1111
private static _1209.Solution1 solution1;
1212
private static _1209.Solution2 solution2;
13+
private static _1209.Solution3 solution3;
1314

1415
@BeforeClass
1516
public static void setup() {
1617
solution1 = new _1209.Solution1();
1718
solution2 = new _1209.Solution2();
19+
solution3 = new _1209.Solution3();
1820
}
1921

2022
@Test
2123
public void test1() {
2224
assertEquals("abcd", solution1.removeDuplicates("abcd", 2));
2325
assertEquals("abcd", solution2.removeDuplicates("abcd", 2));
26+
assertEquals("abcd", solution3.removeDuplicates("abcd", 2));
2427
}
2528

2629
@Test
2730
public void test2() {
2831
assertEquals("aa", solution1.removeDuplicates("deeedbbcccbdaa", 3));
2932
assertEquals("aa", solution2.removeDuplicates("deeedbbcccbdaa", 3));
33+
assertEquals("aa", solution3.removeDuplicates("deeedbbcccbdaa", 3));
3034
}
3135

3236
@Test
3337
public void test3() {
3438
assertEquals("ps", solution1.removeDuplicates("pbbcggttciiippooaais", 2));
3539
assertEquals("ps", solution2.removeDuplicates("pbbcggttciiippooaais", 2));
40+
assertEquals("ps", solution3.removeDuplicates("pbbcggttciiippooaais", 2));
3641
}
3742

3843
@Test
3944
public void test4() {
4045
assertEquals("ghayqgq", solution1.removeDuplicates("ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", 7));
4146
assertEquals("ghayqgq", solution2.removeDuplicates("ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", 7));
47+
assertEquals("ghayqgq", solution3.removeDuplicates("ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", 7));
4248
}
4349

4450
}

0 commit comments

Comments
 (0)
Failed to load comments.