Skip to content

Commit ab3426a

Browse files
committed
🎉 feat: initial commit for leetcode 394 using Swift
1 parent b0a7280 commit ab3426a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Swift/394. Decode String.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 394. Decode String
2+
// 12 ms, 35.71%
3+
func decodeString(_ s: String) -> String {
4+
var stack = [String]()
5+
s.forEach {
6+
switch $0 {
7+
case "]":
8+
var s = "", i = ""
9+
while let c = stack.popLast(), c != "[" {
10+
s = "\(c)\(s)"
11+
}
12+
while let sl = stack.last, nil != Int("\(sl)"), let c = stack.popLast() {
13+
i = "\(c)\(i)"
14+
}
15+
stack.append(String(repeating: s, count: Int(i)!))
16+
default:
17+
stack.append(String($0))
18+
}
19+
}
20+
21+
return stack.joined()
22+
}
23+

0 commit comments

Comments
 (0)