Calculate Digit Sum of a String

Easy

Description

You are given a string s consisting of digits. Return the string after repeatedly replacing all runs of k or more consecutive digits with their sum until length <= k.

Examples

Input:s = "11111222223", k = 3
Output:"135"
Explanation:

111+112+222+23 -> 135.

Input:s = "123456789", k = 4
Output:99
Explanation:

Groups of 4: "1234" (sum=10), "5678" (sum=26), "9" (sum=9). Concatenate: "10269". Groups of 4: "1026" (sum=9), "9" (sum=9). Concatenate: "99". Length 2 <= 4, done.

Input:s = "999888", k = 2
Output:87
Explanation:

Groups of 2: "99"->18, "98"->17, "88"->16. Concatenate: "181716". Groups of 2: "18"->9, "17"->8, "16"->7. Concatenate: "987". Groups of 2: "98"->17, "7"->7. Concatenate: "177". Groups of 2: "17"->8, "7"->7. Concatenate: "87". Length 2 <= k, done.

Constraints

  • 1 ≤ s.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!