File tree Expand file tree Collapse file tree 2 files changed +46
-5
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand Expand file tree Collapse file tree 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) {
115115 return sb .reverse ().toString ();
116116 }
117117 }
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+ }
118156}
Original file line number Diff line number Diff line change 11package com .fishercoder .secondthousand ;
22
33import 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 ;
66
7- import static org .junit .Assert .assertEquals ;
7+ import static org .junit .jupiter . api . Assertions .assertEquals ;
88
99public class _1209Test {
1010
1111 private static _1209 .Solution1 solution1 ;
1212 private static _1209 .Solution2 solution2 ;
1313 private static _1209 .Solution3 solution3 ;
14+ private static _1209 .Solution4 solution4 ;
1415
15- @ BeforeClass
16- public static void setup () {
16+ @ BeforeEach
17+ public void setup () {
1718 solution1 = new _1209 .Solution1 ();
1819 solution2 = new _1209 .Solution2 ();
1920 solution3 = new _1209 .Solution3 ();
21+ solution4 = new _1209 .Solution4 ();
2022 }
2123
2224 @ Test
2325 public void test1 () {
2426 assertEquals ("abcd" , solution1 .removeDuplicates ("abcd" , 2 ));
2527 assertEquals ("abcd" , solution2 .removeDuplicates ("abcd" , 2 ));
2628 assertEquals ("abcd" , solution3 .removeDuplicates ("abcd" , 2 ));
29+ assertEquals ("abcd" , solution4 .removeDuplicates ("abcd" , 2 ));
2730 }
2831
2932 @ Test
You can’t perform that action at this time.
0 commit comments