Leetcode Note: Go - Calculate Digit Sum of a String

Calculate Digit Sum of a String - LeetCode
https://leetcode.com/problems/calculate-digit-sum-of-a-string/

  • Go 言語で取り組んだメモ

回答

Calculate Digit Sum of a String - LeetCode
https://leetcode.com/problems/calculate-digit-sum-of-a-string/solutions/3672884/golang-recursion-runtime-0-ms-beats-100/

func digitSum(s string, k int) string {
    if len(s) <= k { return s }
    result := split(s, k)
    return digitSum(stringCount(result), k)
}

func stringCount(strs []string) (result string) {
    for _, elements := range strs {
        sum := 0 
        for _, item := range elements {
            i, _ := strconv.Atoi(string(item))
            sum += i
        }
        result += fmt.Sprint(sum)
    }
    return
}

func split(s string, k int) (result []string) {
    if len(s) <= k { return []string{s} }
    result = append(result, s[:k])
    result = append(result, split(s[k:], k)...)
    return
}