package main
import "strconv"
func decodeString(s string) string {
if len(s) == 0 {
return ""
}
stack := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
by := s[i]
switch by {
case ']':
cnt := make([]byte, 0, len(s))
//pop string
for stack[len(stack)-1] != '[' {
cnt = append(cnt, stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
//pop [
stack = stack[:len(stack)-1]
//pop count
count := ""
for len(stack) != 0 && stack[len(stack)-1] >= '0' && stack[len(stack)-1] <= '9' {
count = count + string(stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
//reverse count for num
num := 0
for i := len(count) - 1; i >= 0; i-- {
v, _ := strconv.Atoi(string(count[i]))
num = num*10 + v
}
//insert string
for i := 0; i < num; i++ {
for j := len(cnt) - 1; j >= 0; j-- {
stack = append(stack, cnt[j])
}
}
default:
stack = append(stack, by)
}
}
return string(stack)
}
func main() {
}