-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394-DecodeString.go
More file actions
174 lines (152 loc) · 4.94 KB
/
394-DecodeString.go
File metadata and controls
174 lines (152 loc) · 4.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
// 394. Decode String
// Given an encoded string, return its decoded string.
// The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.
// Note that k is guaranteed to be a positive integer.
// You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc.
// Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k.
// For example, there will not be input like 3a or 2[4].
// The test cases are generated so that the length of the output will never exceed 10^5.
// Example 1:
// Input: s = "3[a]2[bc]"
// Output: "aaabcbc"
// Example 2:
// Input: s = "3[a2[c]]"
// Output: "accaccacc"
// Example 3:
// Input: s = "2[abc]3[cd]ef"
// Output: "abcabccdcdcdef"
// Constraints:
// 1 <= s.length <= 30
// s consists of lowercase English letters, digits, and square brackets '[]'.
// s is guaranteed to be a valid input.
// All the integers in s are in the range [1, 300].
import "fmt"
import "strings"
import "unicode"
import "strconv"
func decodeString(s string) string {
res := strings.Builder{}
for i := 0; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
count := 0
for i < len(s) && s[i] != '[' {
count = count * 10 + int(s[i] - '0')
i++
}
startIndex := i + 1
for j := 1; j > 0; i++ {
if s[i + 1] == '[' { j++ }
if s[i + 1] == ']' { j-- }
}
for k := 0; k < count; k++ {
res.WriteString(decodeString(s[startIndex:i]))
}
} else {
res.WriteByte(s[i])
}
}
return res.String()
}
// stack
type Stack []string
func (s *Stack) IsEmpty() bool {
return len(*s) <= 0
}
func(s *Stack) Push(item string){
*s = append(*s , item)
}
func (s *Stack) Pop() (string){
if s.IsEmpty(){
return ""
}
item := (*s)[len(*s)-1]
(*s) = (*s)[:len(*s)-1]
return item
}
func (s *Stack) ToString() string{
result := ""
for !s.IsEmpty(){
result+=s.Pop()
}
return result
}
func (s *Stack) Peek() string{
item := (*s)[len(*s)-1]
return item
}
func decodeString1(s string) string {
var stack Stack
res := ""
isDigit := func (s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
for _,item := range s {
charAt := string(item)
if charAt != "]"{
stack.Push(charAt)
} else {
item := ""
for stack.Peek() != "["{
item = stack.Pop() + item
}
//remove "["
stack.Pop()
digit := ""
for !stack.IsEmpty() && isDigit(stack.Peek()){
digit = stack.Pop() + digit
}
k,_ := strconv.ParseInt(digit, 10,32)
subStr := ""
for i := 0 ; i< int(k); i++{
subStr += item
}
stack.Push(subStr)
}
}
for !stack.IsEmpty(){
res = stack.Pop() + res
}
return res
}
// 外层的解码需要等待内层解码的结果。先扫描的字符还用不上,但不能忘了它们。
// 我们准备由内到外,层层解决[ ],需要保持对字符的记忆,于是用栈。
func decodeString2(s string) string {
numStack, strStack := make([]int, 0), make([]string, 0)
num, res := 0, ""
for _, ch := range s {
if ch >= '0' && ch <= '9' { // 叠加十进制数字
num = num*10 + int(ch-'0')
} else if ch == '[' { // 数字和字符串分别入栈、置空
numStack = append(numStack, num)
num = 0
strStack = append(strStack, res)
res = ""
} else if ch == ']' { // 数字和字符串分别出栈、拼接
count := numStack[len(numStack)-1]
numStack = numStack[:len(numStack)-1]
str := strStack[len(strStack)-1]
strStack = strStack[:len(strStack)-1]
res = str + strings.Repeat(res, count) // !!!当前res*count,再接在str后面
} else { // ['a','z'] || ['A','Z']
res += string(ch) // 叠加字符
}
}
return res
}
func main() {
fmt.Println(decodeString("3[a]2[bc]")) // aaabcbc
fmt.Println(decodeString("3[a2[c]]")) // accaccacc
fmt.Println(decodeString("2[abc]3[cd]ef")) // abcabccdcdcdef
fmt.Println(decodeString1("3[a]2[bc]")) // aaabcbc
fmt.Println(decodeString1("3[a2[c]]")) // accaccacc
fmt.Println(decodeString1("2[abc]3[cd]ef")) // abcabccdcdcdef
fmt.Println(decodeString2("3[a]2[bc]")) // aaabcbc
fmt.Println(decodeString2("3[a2[c]]")) // accaccacc
fmt.Println(decodeString2("2[abc]3[cd]ef")) // abcabccdcdcdef
}