Iterative Digit Summation and Reduction Beats 100% Of the Submissions
IntuitionThe problem requires repeatedly dividing the string into groups of size  k , summing the digits in each group, and forming a new string until the lengt
# Intuition
The problem requires repeatedly dividing the string into groups of size  k , summing the digits in each group, and forming a new string until the length of the string is at most  k . This suggests an iterative approach where we repeatedly process the string, reducing its length each time.

# Approach
1. Iterate While Length is Greater than  k :
- Continue processing the string until its length becomes less than equal to k.
2. Grouping and Summation:
- Traverse the string and sum up digits in groups of size  k .
- If the last group is smaller than  k , process it separately.
3. Forming the New String:
- Convert the sum of each group into a string and concatenate to form the new string.
- Repeat the process until the length constraint is met.

The approach ensures that the string is reduced efficiently in each round until it satisfies the problem condition.

# Complexity
- Time complexity: $$O(n)$$ per iteration, where  n  is the length of the string. Since the string shrinks after every iteration, the number of iterations is logarithmic in  n . The worst-case complexity is approximately  $$O(nlogn)$$.

- Space complexity: $$O(n)$$ for storing the intermediate strings.

# Code
```cpp []
class Solution {
public:
    string digitSum(string s, int k) {
        while (s.size()>k){
            string b="";
            for (int i=0,l=0;i<s.size();i++){
                l+=s[i]-'0';
                if (((i+1)%k==0)||(i==s.size()-1)){b+=to_string(l);l=0;}
            }
            s=b;
        }
        return s;
    }
};
```
['C++']