Skip to content

Commit 21f4407

Browse files
authored
Create 0394-decode-string.kt
1 parent 74ed41c commit 21f4407

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: kotlin/0394-decode-string.kt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun decodeString(s: String): String {
3+
val stack = LinkedList<String>()
4+
5+
for (c in s) {
6+
if (c != ']') {
7+
stack.addLast(c.toString())
8+
} else {
9+
val sb = StringBuilder()
10+
while (stack.isNotEmpty() && stack.peekLast() != "[")
11+
sb.insert(0, stack.removeLast())
12+
stack.removeLast()
13+
14+
val k = StringBuilder()
15+
while (stack.isNotEmpty() && stack.peekLast().all { char -> char.isDigit() })
16+
k.insert(0, stack.removeLast())
17+
18+
val times = k.toString().toInt()
19+
stack.addLast(sb.toString().repeat(times))
20+
}
21+
}
22+
23+
return stack.joinToString("")
24+
}
25+
}

0 commit comments

Comments
 (0)