EASY CPP Solution | | Beats 100% in Runtime | | Beginner Friendly
\n\n\n# Code\n\nclass Solution {\npublic:\n    string digitSum(string s, int k) {\n        string ans = s;\n        while(ans.length() > k){\n            string
![image.png](https://assets.leetcode.com/users/images/bb105cdb-8bce-4d0b-a2fd-7132ca65ea85_1695322421.7062838.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    string digitSum(string s, int k) {\n        string ans = s;\n        while(ans.length() > k){\n            string tmp = "";\n            int num = 0, cnt=0;\n            for(int i=0; i<ans.length(); i++){\n                num += ans[i] - \'0\';\n                cnt++;\n                if(i != 0 && cnt == k || i == ans.length()-1){\n                    tmp += to_string(num);\n                    num = cnt = 0;\n                }\n            }\n            ans = tmp;\n        }\n        return ans;\n    }\n};\n```
['C++']