Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 13, 2023
1 parent f2745f2 commit 724df9a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions leetcode/leetcode75/Stack/394.DecodeString/decodeString.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
)

func decodeString(s string) string {
s1, s2 := []int{}, []string{}
num, result := 0, ""

for i := 0; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
num = num*10 + int(s[i]-'0')
} else if s[i] == '[' {
s1 = append(s1, num)
s2 = append(s2, result)
num = 0
result = ""
} else if s[i] == ']' {
n := s1[len(s1)-1]
s1 = s1[:len(s1)-1]
temp := ""
for i := 0; i < n; i++ {
temp += result
}
result = s2[len(s2)-1]
s2 = s2[:len(s2)-1]
result += temp
} else {
result += string(s[i])
}
}
return result
}

func main() {
s := "3[a2[c]]"
fmt.Println(decodeString(s))
}

0 comments on commit 724df9a

Please sign in to comment.