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 d09d906 commit ef0fa16Copy full SHA for ef0fa16
leetcode-scala/src/main/scala/LeetCode75/Stack/394. Decode String.sc
@@ -0,0 +1,28 @@
1
+object Solution {
2
+ def decodeString(s: String): String = {
3
+ val stack = scala.collection.mutable.Stack.empty[String]
4
+ for (c <- s) {
5
+ if (c != ']') {
6
+ stack.push(c.toString)
7
+ } else {
8
+ var s = ""
9
+ while (stack.top != "[") {
10
+ s += stack.pop()
11
+ }
12
+ stack.pop()
13
+ var n = ""
14
+ while (stack.nonEmpty && stack.top.toIntOption.nonEmpty) {
15
+ n = stack.pop() + n
16
17
+ stack.push(s * n.toInt)
18
19
20
+ stack.mkString.reverse
21
22
+}
23
+
24
25
+Solution.decodeString("3[a]2[bc]") // "aaabcbc"
26
+Solution.decodeString("3[a2[c]]") // "accaccacc"
27
+Solution.decodeString("2[abc]3[cd]ef") // "abcabccdcdcdef"
28
0 commit comments