Reduce string by performing repeated digit sum in group of K
Last Updated :
08 May, 2023
Given a string S of length N consisting of digits and an integer K, Reduce the string by performing the following operation till the length of the string is greater than K:
- Divide the string 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.
- Merge the digit sum of consecutive groups together to form a new string. If the length of the string is greater than K, repeat the steps.
Examples:
Input: S = “11111222223”, K = 3
Output: “135”
Explanation:
For the first round, divide S into groups of size 3: “111”, “112”, “222”, and “23”.
Then calculate the digit sum of each group:
1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
So, string becomes “3” + “4” + “6” + “5” = “3465”.
For the second round, divide s into “346” and “5”.
Then calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
So, string becomes “13” + “5” = “135”.
After second round. Now, length of S <= K, so return “135” as the answer.
Input: S = “1123”, K = 2
Output: “25”
Explanation: For the first round, divide S into groups of size 2: “11”, “23”. ​
Then we calculate the digit sum of each group: 1 + 1 = 2 and 2 + 3 = 5.
So, String becomes “2” + “5” = “25” after the first round.
Now, length of S <= K, so return “25” as the answer.
Approach: This is a simple implementation based problem. The solution is as follows:
Iterate S until its size is greater than K. In every iteration, divide the string in consecutive K sized groups, get their sum and add them to form the new string.
Follow the below steps to implement the idea:
- Run a loop until the given string S has length greater than K.
- Take one new string (say temp) to store the new string.
- Traverse string S from i = 0 to i = s.size() – 1.
- Define a variable (say sum = 0) to store the sum.
- Iterate in a group of K characters and keep storing the digit sum of K digits.
- Append the sum in temp string.
- Make S = temp, i.e. same as the newly formed string.
- Return S as the final string.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string digitSum(string s, int k)
{
while (s.size() > k) {
string temp = "" ;
for ( int i = 0; i < s.size(); i++) {
int sum = 0;
int count = 0;
while (i < s.size() && count < k) {
sum += s[i++] - '0' ;
count++;
}
temp += to_string(sum);
i--;
}
s = temp;
}
return s;
}
int main()
{
string S = "11111222223" ;
int K = 3;
string ans = digitSum(S, K);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static String digitSum(String s, int k)
{
while (s.length() > k) {
String temp = "" ;
for ( int i = 0 ; i < s.length(); i++) {
int sum = 0 ;
int count = 0 ;
while (i < s.length() && count < k) {
sum += s.charAt(i++) - '0' ;
count++;
}
temp += Integer.toString(sum);
i--;
}
s = temp;
}
return s;
}
public static void main(String[] args)
{
String S = "11111222223" ;
int K = 3 ;
String ans = digitSum(S, K);
System.out.println(ans);
}
}
|
Python3
def digitSum(s,k):
while ( len (s)>k):
temp = ""
i = 0
while (i< len (s)):
Sum = 0
count = 0
while (i< len (s) and count<k):
Sum = Sum + int (s[i])
i = i + 1
count = count + 1
temp = temp + str ( Sum )
i = i - 1
i = i + 1
s = temp
return s
S = "11111222223"
K = 3
ans = digitSum(S,K)
print (ans)
|
C#
using System;
class GFG
{
public static string digitSum( string s, int k)
{
while (s.Length > k) {
string temp = "" ;
for ( int i = 0; i < s.Length; i++) {
int sum = 0;
int count = 0;
while (i < s.Length && count < k) {
sum += s[i++] - '0' ;
count++;
}
temp += sum;
i--;
}
s = temp;
}
return s;
}
public static void Main()
{
string S = "11111222223" ;
int K = 3;
String ans = digitSum(S, K);
Console.Write(ans);
}
}
|
Javascript
<script>
function digitSum(s, k)
{
while (s.length > k) {
let temp = "" ;
for (let i = 0; i < s.length; i++) {
let sum = 0;
let count = 0;
while (i < s.length && count < k) {
sum += s[i++] - '0' ;
count++;
}
temp += sum.toString();
i--;
}
s = temp;
}
return s;
}
let S = "11111222223" ;
let K = 3;
let ans = digitSum(S, K);
document.write(ans);
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...