2243. Calculate Digit Sum of a String LeetCode Solution

In this guide, you will get 2243. Calculate Digit Sum of a String LeetCode Solution with the best time and space complexity. The solution to Calculate Digit Sum of a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Calculate Digit Sum of a String solution in C++
  4. Calculate Digit Sum of a String solution in Java
  5. Calculate Digit Sum of a String solution in Python
  6. Additional Resources
2243. Calculate Digit Sum of a String LeetCode Solution image

Problem Statement of 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:

Input: s = “11111222223”, k = 3
Output: “135”
Explanation:
– For the first round, we divide s into groups of size 3: “111”, “112”, “222”, and “23”.
​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
So, s becomes “3” + “4” + “6” + “5” = “3465” after the first round.
– For the second round, we divide s into “346” and “5”.
Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
So, s becomes “13” + “5” = “135” after second round.
Now, s.length <= k, so we return "135" as the answer.

See also  392. Is Subsequence LeetCode Solution

Example 2:

Input: s = "00000000", k = 3
Output: "000"
Explanation:
We divide s into "000", "000", and "00".
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2243. Calculate Digit Sum of a String LeetCode Solution in C++

class Solution {
 public:
  string digitSum(string s, int k) {
    while (s.length() > k) {
      string next;
      for (int i = 0; i < s.length(); ++i)
        next += to_string(accumulate(
            s.begin() + i, s.begin() + min(static_cast<int>(s.length()), i + k),
, [](int subtotal, char c) { return subtotal + c - '0'; }));
      s = std::move(next);
    }
    return s;
  }
};
/* code provided by PROGIEZ */

2243. Calculate Digit Sum of a String LeetCode Solution in Java

class Solution {
  public String digitSum(String s, int k) {
    while (s.length() > k) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < s.length(); i += k) {
        int sum = 0;
        for (int j = i; j < Math.min(s.length(), i + k); ++j)
          sum += s.charAt(j) - '0';
        sb.append(sum);
      }
      s = sb.toString();
    }
    return s;
  }
}
// code provided by PROGIEZ

2243. Calculate Digit Sum of a String LeetCode Solution in Python

class Solution:
  def digitSum(self, s: str, k: int) -> str:
    while len(s) > k:
      next = []
      for i in range(0, len(s), k):
        summ = 0
        for j in range(i, min(len(s), i + k)):
          summ += int(s[j])
        next.append(str(summ))
      s = ''.join(next)
    return s
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.