Files
LeetCode-Go/leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go
2020-08-07 17:06:53 +08:00

30 lines
500 B
Go

package leetcode
func isLetter(char byte) bool {
if char >= 'a' && char <= 'z' {
return true
}
return false
}
func decodeAtIndex(S string, K int) string {
length := 0
for i := 0; i < len(S); i++ {
if isLetter(S[i]) {
length++
if length == K {
return string(S[i])
}
} else {
if length*int(S[i]-'0') >= K {
if K%length != 0 {
return decodeAtIndex(S[:i], K%length)
}
return decodeAtIndex(S[:i], length)
}
length *= int(S[i] - '0')
}
}
return ""
}