-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecodeString.swift
More file actions
57 lines (53 loc) · 1.74 KB
/
Copy pathDecodeString.swift
File metadata and controls
57 lines (53 loc) · 1.74 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// DecodeString.swift
// ConquerAlgorithm
//
// Created by Freelf on 2021/3/30.
// Copyright © 2021 Freelf. All rights reserved.
//
import Foundation
class DecodeStringSolution {
func decodeString(_ s: String) -> String {
var stack: [(Int, String)] = []
var result = ""
var mutil = 0
for char in s {
if char == "[" {
stack.append((mutil, result))
result = ""
mutil = 0
} else if char == "]" {
let (curMutil, lastRes) = stack.removeLast()
result = lastRes + String.init(repeating: result, count: curMutil)
} else if char.isWholeNumber {
mutil = mutil * 10 + char.wholeNumberValue!
} else {
result += String(char)
}
}
return result
}
func decodeString2(_ s: String) -> String {
func dfs(_ s: String, _ i: Int) -> (String, Int){
var res = "", mutil = 0, index = i
while index < s.count {
let curChar = s[s.index(s.startIndex, offsetBy: index)]
if curChar == "[" {
let (result, returnIndex) = dfs(s, index + 1)
res += String.init(repeating: result, count: mutil)
index = returnIndex
mutil = 0
} else if curChar == "]" {
return (res, index)
} else if curChar.isWholeNumber {
mutil = mutil * 10 + curChar.wholeNumberValue!
} else {
res.append(curChar)
}
index += 1
}
return (res, -1)
}
return dfs(s, 0).0
}
}