2025-08-17 14:04:01 +0000 UTC

Divide a String Into Groups of Size k

Code

class Solution:
    def divideString(self, s: str, k: int, fill: str) -> List[str]:
        res = []
        cur = []
        for char in s:
            cur.append(char)
            if len(cur) == k:
                res.append("".join(cur))
                cur.clear()
        if cur and len(cur) < k:
            cur.append(fill * (k - len(cur)))
            res.append("".join(cur))
            cur.clear()
        return res