forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0394-decode-string.scala
29 lines (26 loc) · 911 Bytes
/
0394-decode-string.scala
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
objectSolution {
defdecodeString(s: String):String= {
val (str, res) = decodeStringRecursive(s, 0)
str
}
defdecodeStringRecursive(input: String, start: Int): (String, Int) = {
valresult=newStringBuilder()
vari= start
while(i < input.length){
input(i) match {
case x if x>='0'&& x<='9'=>
valcurrNum= 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)
}
}