304 North Cardinal St.
Dorchester Center, MA 02124

# Calculate Digit Sum of a String LeetCode Solution

## Problem – Calculate Digit Sum of a String LeetCode Solution

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

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

1. 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`.
2. 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`.
3. 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.
``````

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.

### Calculate Digit Sum of a String LeetCode Solution in C++

``````string digitSum(string s, int k) {
string ans;
while(1){

if(s.length()<=k)   return s;

int sum=0;
for(int i=0;i<s.size();i++){
if(i!=0 and i%k==0){
ans+=to_string(sum);
sum=0;
}
sum+=(s[i]-'0');
}//end of for

ans+=to_string(sum);
s=ans;
ans="";

}//end of while
}
``````

### Calculate Digit Sum of a String LeetCode Solution in Java

``````class Solution {
public String digitSum(String s, int k) {
while(s.length()>k){
String ns=""; // replace the old string with updated one
for(int i=0;i<s.length();i+=k){
String t=s.substring(i,Math.min(i+k,s.length())); // form the string of k size
int sum=0;
for(int l=0;l<t.length();l++){ // to find the character sum of string t
sum+=t.charAt(l)-'0';
}
ns+="" + sum;    //update the string with sum of k size string character
}
s=ns; //update the old string with updated one
}
return s;
}
}
``````

### Calculate Digit Sum of a String LeetCode Solution in Python

``````class Solution:
def digitSum(self, s: str, k: int) -> str:
def divideString(s: str, k: int) -> List[str]: # Utility function to return list of divided groups.
l, n = [], len(s)
for i in range(0, n, k):
l.append(s[i:min(i + k, n)])
return l
while len(s)>k: # Till size of s is greater than k
arr, temp = divideString(s, k), [] # arr is the list of divided groups, temp will be the list of group sums
for group in arr: # Traverse the group and add its digits
group_sum = 0
for digit in group:
group_sum += int(digit)
temp.append(str(group_sum)) # Sum of digits of current group
s = ''.join(temp) # s is replaced by the group digit sum for each group.
return s
``````
##### Calculate Digit Sum of a String LeetCode Solution Review:

In our experience, we suggest you solve this Calculate Digit Sum of a String LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Calculate Digit Sum of a String LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Calculate Digit Sum of a String LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions