We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 98d1347 commit f2292b1Copy full SHA for f2292b1
394.DecodeString.kt
@@ -0,0 +1,32 @@
1
+//https://leetcode.com/problems/decode-string/description
2
+fun decodeString(s: String): String {
3
+ val stack = mutableListOf<String>()
4
+ var currNum = 0
5
+ var currString = ""
6
+
7
+ for (char in s) {
8
+ when (char) {
9
+ in '0'..'9' -> {
10
+ currNum = currNum * 10 + (char - '0')
11
+ }
12
13
+ '[' -> {
14
+ stack.add(currString)
15
+ stack.add(currNum.toString())
16
+ currNum = 0
17
+ currString = ""
18
19
20
+ ']' -> {
21
+ val num = stack.removeLast().toInt()
22
+ val prevString = stack.removeLast()
23
+ currString = prevString + currString.repeat(num)
24
25
26
+ else -> currString += char
27
28
29
30
+ return currString
31
32
+}
0 commit comments