File tree 2 files changed +46
-5
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand
2 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -115,4 +115,42 @@ public String removeDuplicates(String s, int k) {
115
115
return sb .reverse ().toString ();
116
116
}
117
117
}
118
+
119
+ public static class Solution4 {
120
+ //my completely original solution on 6/19/2024
121
+ public String removeDuplicates (String s , int k ) {
122
+ Deque <Pair > stack = new LinkedList <>();
123
+ for (char c : s .toCharArray ()) {
124
+ if (!stack .isEmpty () && stack .peekLast ().c == c ) {
125
+ Pair pair = stack .pollLast ();
126
+ pair .count = pair .count + 1 ;
127
+ if (pair .count < k ) {
128
+ stack .addLast (pair );
129
+ }
130
+ } else {
131
+ stack .addLast (new Pair (c , 1 ));
132
+ }
133
+ }
134
+ StringBuilder sb = new StringBuilder ();
135
+ while (!stack .isEmpty ()) {
136
+ Pair pair = stack .pollLast ();
137
+ int count = pair .count ;
138
+ while (count -- > 0 ) {
139
+ sb .append (pair .c );
140
+ }
141
+ }
142
+ return sb .reverse ().toString ();
143
+ }
144
+
145
+ class Pair {
146
+ char c ;
147
+ int count ;
148
+
149
+ public Pair (char c , int count ) {
150
+ this .c = c ;
151
+ this .count = count ;
152
+ }
153
+ }
154
+
155
+ }
118
156
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .secondthousand ;
2
2
3
3
import com .fishercoder .solutions .secondthousand ._1209 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _1209Test {
10
10
11
11
private static _1209 .Solution1 solution1 ;
12
12
private static _1209 .Solution2 solution2 ;
13
13
private static _1209 .Solution3 solution3 ;
14
+ private static _1209 .Solution4 solution4 ;
14
15
15
- @ BeforeClass
16
- public static void setup () {
16
+ @ BeforeEach
17
+ public void setup () {
17
18
solution1 = new _1209 .Solution1 ();
18
19
solution2 = new _1209 .Solution2 ();
19
20
solution3 = new _1209 .Solution3 ();
21
+ solution4 = new _1209 .Solution4 ();
20
22
}
21
23
22
24
@ Test
23
25
public void test1 () {
24
26
assertEquals ("abcd" , solution1 .removeDuplicates ("abcd" , 2 ));
25
27
assertEquals ("abcd" , solution2 .removeDuplicates ("abcd" , 2 ));
26
28
assertEquals ("abcd" , solution3 .removeDuplicates ("abcd" , 2 ));
29
+ assertEquals ("abcd" , solution4 .removeDuplicates ("abcd" , 2 ));
27
30
}
28
31
29
32
@ Test
You can’t perform that action at this time.
0 commit comments