Calculate Digit Sum of a String - Problem

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

Your task is to repeatedly process the string in rounds until its length becomes โ‰ค k. Each round involves:

  1. Divide the string into consecutive groups of size k (the last group may be smaller)
  2. Replace each group with the sum of its digits. For example, "346" becomes "13" (3+4+6=13)
  3. Merge all groups to form a new string
  4. Repeat if the new string length > k

Think of it like condensing a long number by repeatedly summing chunks until it fits your desired size!

Example: If s="11111222223" and k=3, we get groups ["111", "112", "222", "23"], which become ["3", "4", "6", "5"], merged as "3465". Since len("3465") > 3, we continue...

Input & Output

example_1.py โ€” Python
$ Input: s = "11111222223", k = 3
โ€บ Output: "135"
๐Ÿ’ก Note: Round 1: Groups ["111","112","222","23"] become [3,4,6,5] โ†’ "3465". Round 2: Groups ["346","5"] become [13,5] โ†’ "135". Since len("135") = 3 โ‰ค k, we stop.
example_2.py โ€” Python
$ Input: s = "00000000", k = 3
โ€บ Output: "000"
๐Ÿ’ก Note: Round 1: Groups ["000","000","00"] become [0,0,0] โ†’ "000". Since len("000") = 3 โ‰ค k, we stop.
example_3.py โ€” Python
$ Input: s = "123", k = 3
โ€บ Output: "123"
๐Ÿ’ก Note: Since len("123") = 3 โ‰ค k, no processing needed. Return original string.

Visualization

Tap to expand
Digital String Compression ProcessOriginal String: "11111222223" (length=11 > k=3)1 1 1 1 1 2 2 2 2 2 3Round 1: Group into chunks of size k=31 1 1Sum: 31 1 2Sum: 42 2 2Sum: 62 3Sum: 5"3465"Length: 4 > k=3, continueRound 2: Process "3465" (length=4 > k=3)3 4 6Sum: 135Sum: 5"135"Length: 3 = k, STOP!Final Result:"135"โœ… String length โ‰ค kโœ… Compression completeโœ… Return final result๐ŸŽฏ Key Insight: Each round dramatically reduces string length, so very few rounds are needed!
Understanding the Visualization
1
Initial Setup
Start with a long string of digits that exceeds our size limit k
2
Group Formation
Divide the string into consecutive groups of size k (last group may be smaller)
3
Digit Summation
Calculate the sum of all digits in each group
4
Compression
Replace each group with its digit sum, creating a shorter string
5
Iteration Check
If the new string is still too long, repeat the process
Key Takeaway
๐ŸŽฏ Key Insight: The string length reduces dramatically in each round (typically by factor of k/2 or more), making this algorithm very efficient with O(n) total time complexity despite the iterative nature.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

While we have rounds, each character is processed a constant number of times since string length reduces dramatically each round

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for storing intermediate strings during processing

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • 1 โ‰ค k โ‰ค 100
  • s consists of only digits
  • k โ‰ค s.length (at least one round is possible if needed)
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen