Calculate Digit Sum of a String

You are given a string s consisting of digits and an integer k.

A round can be completed if the length of s is greater than k. In one round, do the following:

  • Divide s into consecutive groups of size k such that the first k characters are in the first group, the next k characters are in the second group, and so on. Note that the size of the last group can be smaller than k.
  • Replace each group of s with a string representing the sum of all its digits. For example, "346" is replaced with "13" because 3 + 4 + 6 = 13.
  • Merge consecutive groups together to form a new string. If the length of the string is greater than k, repeat from step 1.

Return s after all rounds have been completed.

Example 1
Inputs = "11111222223", k = 3
Output"135"
After two rounds, s becomes "135", and its length is no longer greater than k.
Example 2
Inputs = "00000000", k = 3
Output"000"
The groups "000", "000", and "00" each have digit sum 0, producing "000", whose length is equal to k.

Constraints

  • 1 <= s.length <= 100
  • 2 <= k <= 100
  • s consists of digits only.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate