2243. Calculate Digit Sum of a String

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2243

class Solution:
    def digitSum(self, s: str, k: int) -> str:
        if len(s) <= k:
            return s
        t = []
        while s:
            t.append(str(sum(int(v) for v in s[:k])))
            s = s[k:]
        return self.digitSum(''.join(t), k)