Leetcode Note: Go - Divide a String Into Groups of Size K

Divide a String Into Groups of Size k - LeetCode
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/

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

回答

Go - Divide a String Into Groups of Size k - LeetCode
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/solutions/3246332/go/

func divideString(s string, k int, fill byte) []string {
    result := []string{}

    lenS := len(s)
    for i := 0; i < lenS; i += k {
        if lenS < i + k {
            fillCount := k - (lenS - i)

            return append(result, s[i:] + strings.Repeat(string(fill), fillCount)) 
        }

        result = append(result, s[i:i+k])
    }

    return result
}