Skip to content

Commit

Permalink
Time: 1 ms (75.07%), Space: 2.3 MB (83.19%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Sep 8, 2024
1 parent e0e7709 commit 643adff
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions 0394-decode-string/0394-decode-string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ func decodeString(s string) string {
nums, codes := []int{}, []string{}
num, code := "", ""
for _, c := range s {
if unicode.IsNumber(c) {
if unicode.IsDigit(c) {
num += string(c)
} else if unicode.IsLetter(c) {
code += string(c)
} else if c == '[' {
n, _ := strconv.Atoi(num)
nums, codes = append(nums, n), append(codes, code)
nums = append(nums, n)
codes = append(codes, code)
num, code = "", ""
} else if c == ']' {
n, c := nums[len(nums)-1], codes[len(codes)-1]
code = c + strings.Repeat(code, n)
nums, codes = nums[:len(nums)-1], codes[:len(codes)-1]
}
}

return code
}

0 comments on commit 643adff

Please sign in to comment.