We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 74ed41c commit 21f4407Copy full SHA for 21f4407
kotlin/0394-decode-string.kt
@@ -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