forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0394-decode-string.scala
More file actions
29 lines (26 loc) · 911 Bytes
/
0394-decode-string.scala
File metadata and controls
29 lines (26 loc) · 911 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
26
27
28
29
object Solution {
def decodeString(s: String): String = {
val (str, res) = decodeStringRecursive(s, 0)
str
}
def decodeStringRecursive(input: String, start: Int): (String, Int) = {
val result = new StringBuilder()
var i = start
while(i < input.length){
input(i) match {
case x if x>= '0' && x<='9' =>
val currNum = input.substring(i, input.indexOf('[', i)).toInt
val (str, maxI) = decodeStringRecursive(input, input.indexOf('[', i)+1)
i = maxI
result.append(str * currNum)
case x if x >= 'a' && x <= 'z' =>
(result.append(x), i)
case ']' =>
return (result.toString, i)
case _ =>
}
i+=1
}
(result.toString, i)
}
}