forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0394-decode-string.kt
More file actions
25 lines (21 loc) · 783 Bytes
/
0394-decode-string.kt
File metadata and controls
25 lines (21 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
fun decodeString(s: String): String {
val stack = LinkedList<String>()
for (c in s) {
if (c != ']') {
stack.addLast(c.toString())
} else {
val sb = StringBuilder()
while (stack.isNotEmpty() && stack.peekLast() != "[")
sb.insert(0, stack.removeLast())
stack.removeLast()
val k = StringBuilder()
while (stack.isNotEmpty() && stack.peekLast().all { char -> char.isDigit() })
k.insert(0, stack.removeLast())
val times = k.toString().toInt()
stack.addLast(sb.toString().repeat(times))
}
}
return stack.joinToString("")
}
}