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
sinto consecutive groups of sizeksuch that the firstkcharacters are in the first group, the nextkcharacters are in the second group, and so on. Note that the size of the last group can be smaller thank. - Replace each group of
swith a string representing the sum of all its digits. For example,"346"is replaced with"13"because3 + 4 + 6 = 13. - Merge consecutive groups together to form a new string. If the length of the string is greater than
k, repeat from step1.
Return s after all rounds have been completed.
Example 1
Input
s = "11111222223", k = 3Output
"135"After two rounds,
s becomes "135", and its length is no longer greater than k.Example 2
Input
s = "00000000", k = 3Output
"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.