Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf3bc0e2

Browse files
add a solution for 1209
1 parent79deb72 commitf3bc0e2

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
packagecom.fishercoder.solutions;
22

3+
importjava.util.Deque;
4+
importjava.util.LinkedList;
35
importjava.util.Stack;
46

57
publicclass_1209 {
@@ -72,4 +74,45 @@ public String removeDuplicates(String s, int k) {
7274
returnsb.toString();
7375
}
7476
}
77+
78+
publicstaticclassSolution3 {
79+
/**
80+
* My completely original solution on 1/6/2021.
81+
*/
82+
classCharCount {
83+
charc;
84+
intcount;
85+
86+
publicCharCount(charc,intcount) {
87+
this.c =c;
88+
this.count =count;
89+
}
90+
}
91+
92+
publicStringremoveDuplicates(Strings,intk) {
93+
Deque<CharCount>stack =newLinkedList<>();
94+
for (charc :s.toCharArray()) {
95+
if (stack.isEmpty()) {
96+
stack.addLast(newCharCount(c,1));
97+
}else {
98+
if (stack.peekLast().c ==c &&stack.peekLast().count +1 ==k) {
99+
stack.pollLast();
100+
}elseif (stack.peekLast().c ==c) {
101+
stack.addLast(newCharCount(c,stack.pollLast().count +1));
102+
}else {
103+
stack.addLast(newCharCount(c,1));
104+
}
105+
}
106+
}
107+
StringBuildersb =newStringBuilder();
108+
while (!stack.isEmpty()) {
109+
CharCountpair =stack.pollLast();
110+
intcount =pair.count;
111+
while (count-- >0) {
112+
sb.append(pair.c);
113+
}
114+
}
115+
returnsb.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
privatestatic_1209.Solution1solution1;
1212
privatestatic_1209.Solution2solution2;
13+
privatestatic_1209.Solution3solution3;
1314

1415
@BeforeClass
1516
publicstaticvoidsetup() {
1617
solution1 =new_1209.Solution1();
1718
solution2 =new_1209.Solution2();
19+
solution3 =new_1209.Solution3();
1820
}
1921

2022
@Test
2123
publicvoidtest1() {
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
publicvoidtest2() {
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
publicvoidtest3() {
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
publicvoidtest4() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp