Skip to content

Commit 12c1840

Browse files
authored
Create 1209-remove-all-adjacent-duplicates-in-string-ii.kt
1 parent 21f4407 commit 12c1840

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun removeDuplicates(s: String, k: Int): String {
3+
val stack = LinkedList<Pair<Char, Int>>()
4+
5+
for (c in s) {
6+
if (stack.isNotEmpty() && stack.peekLast().first == c) {
7+
val (ch, co) = stack.removeLast()
8+
stack.addLast(c to (co + 1))
9+
} else {
10+
stack.addLast(c to 1)
11+
}
12+
13+
if (stack.isNotEmpty() && stack.peekLast().second == k) {
14+
stack.removeLast()
15+
}
16+
}
17+
18+
val res = StringBuilder()
19+
for ((ch, co) in stack) {
20+
repeat (co) {
21+
res.append(ch)
22+
}
23+
}
24+
25+
return res.toString()
26+
}
27+
}

0 commit comments

Comments
 (0)