[C#] | O(N) | 0 ms | 100% | Best Solution
Approach
First, let's loop our algorithm until s.Length > k. (Step 3)
Use StringBuilder because it's faster than string concatenation. For performance, you can
# Approach
- First, let's loop our algorithm until `s.Length > k`. (Step 3)
- Use `StringBuilder` because it's faster than string concatenation. For performance, you can set Capacity.
- First cycle - required to save the position after the group sum calculation.
- The variables `sum` and `j` are declared in the first cycle. (`j` is declared outside the second cycle because it's value is passed to `i` after the cycle)
- The second cycle - required to calculate the sum of numbers in the group `k`. (`'0'` in ASCII table = 48) 
- Since the last group can be smaller than `k`, check that `j` is smaller than the `s.Length`. 
- Add `sum` to `StringBuilder` and assign the position `j` to `i`.
- Then assign your string from `StringBuilder` to `s`.
- And finally return `s`.
# Complexity
- Time complexity: **O(N)**

- Space complexity: **O(1)**

# Code
```csharp []
public class Solution 
{
    public string DigitSum(string s, int k) 
    {
        while (s.Length > k) 
        {
            StringBuilder builder = new StringBuilder(20);
            for (int i = 0; i < s.Length;)
            {
                int sum = 0, j = i;
                for (; j < s.Length && j < i + k; j++)
                {
                    sum += s[j] - 48;
                }
                builder.Append(sum);
                i = j;
            }
            s = builder.ToString();
        }
        return s;
    }
}
```
---
Thank you for reading my post. Please upvote it! ✔️
---
['Array', 'Two Pointers', 'String', 'C#']