Check if a given string is sum-string
Given a string of digits, determine whether it is a ‘sum-string’. A string S is called a sum-string if a rightmost substring can be written as sum of two substrings before it and same is recursively true for substrings before it. Examples :
“12243660” is a sum string. Explanation : 24 + 36 = 60, 12 + 24 = 36 “1111112223” is a sum string. Explanation: 111+112 = 223, 1+111 = 112 “2368” is not a sum string
In general a string S is called sum-string if it satisfies the following properties:
sub-string(i, x) + sub-string(x+1, j) = sub-string(j+1, l) and sub-string(x+1, j)+sub-string(j+1, l) = sub-string(l+1, m) and so on till end.
From the examples, we can see that our decision depends on first two chosen numbers. So we choose all possible first two number for given string. Then for every chosen two numbers we check whether it is sum-string or not? So the approach is very simple. We generate all possible first two numbers using two substrings s1 and s2 using two loops. then we check whether it is possible to make number s3 = (s1 + s2) or not. If we can make s3 then we recursively check for s2 + s3 so on.
CPP
// C++ program to check if a given string// is sum-string or not#include <bits/stdc++.h>using namespace std;// this is function for finding sum of two// numbers as stringstring string_sum(string str1, string str2){ if (str1.size() < str2.size()) swap(str1, str2); int m = str1.size(); int n = str2.size(); string ans = ""; // sum the str2 with str1 int carry = 0; for (int i = 0; i < n; i++) { // Sum of current digits int ds = ((str1[m - 1 - i] - '0') + (str2[n - 1 - i] - '0') + carry) % 10; carry = ((str1[m - 1 - i] - '0') + (str2[n - 1 - i] - '0') + carry) / 10; ans = char(ds + '0') + ans; } for (int i = n; i < m; i++) { int ds = (str1[m - 1 - i] - '0' + carry) % 10; carry = (str1[m - 1 - i] - '0' + carry) / 10; ans = char(ds + '0') + ans; } if (carry) ans = char(carry + '0') + ans; return ans;}// Returns true of two substrings of given// lengths of str[beg..] can cause a positive// result.bool checkSumStrUtil(string str, int beg, int len1, int len2){ // Finding two substrings of given lengths // and their sum string s1 = str.substr(beg, len1); string s2 = str.substr(beg + len1, len2); string s3 = string_sum(s1, s2); int s3_len = s3.size(); // if number of digits s3 is greater then // the available string size if (s3_len > str.size() - len1 - len2 - beg) return false; // we got s3 as next number in main string if (s3 == str.substr(beg + len1 + len2, s3_len)) { // if we reach at the end of the string if (beg + len1 + len2 + s3_len == str.size()) return true; // otherwise call recursively for n2, s3 return checkSumStrUtil(str, beg + len1, len2, s3_len); } // we do not get s3 in main string return false;}// Returns true if str is sum string, else false.bool isSumStr(string str){ int n = str.size(); // choosing first two numbers and checking // whether it is sum-string or not. for (int i = 1; i < n; i++) for (int j = 1; i + j < n; j++) if (checkSumStrUtil(str, 0, i, j)) return true; return false;}// Driver codeint main(){ cout << isSumStr("1212243660") << endl; cout << isSumStr("123456787"); return 0;} |
Python3
# Python code for the above approach# this is function for finding sum of two# numbers as stringdef string_sum(str1, str2): if (len(str1) < len(str2)): str1, str2 = str2,str1 m = len(str1) n = len(str2) ans = "" # sum the str2 with str1 carry = 0 for i in range(n): # Sum of current digits ds = ((ord(str1[m - 1 - i]) - ord('0')) + (ord(str2[n - 1 - i]) - ord('0')) + carry) % 10 carry = ((ord(str1[m - 1 - i]) - ord('0')) + (ord(str2[n - 1 - i]) - ord('0')) + carry) // 10 ans = str(ds) + ans for i in range(n,m): ds = (ord(str1[m - 1 - i]) - ord('0') + carry) % 10 carry = (ord(str1[m - 1 - i]) - ord('0') + carry) // 10 ans = str(ds) + ans if (carry): ans = str(carry) + ans return ans# Returns True of two substrings of given# lengths of str[beg..] can cause a positive# result.def checkSumStrUtil(Str, beg,len1, len2): # Finding two substrings of given lengths # and their sum s1 = Str[beg: beg+len1] s2 = Str[beg + len1: beg + len1 +len2] s3 = string_sum(s1, s2) s3_len = len(s3) # if number of digits s3 is greater then # the available string size if (s3_len > len(Str) - len1 - len2 - beg): return False # we got s3 as next number in main string if (s3 == Str[beg + len1 + len2: beg + len1 + len2 +s3_len]): # if we reach at the end of the string if (beg + len1 + len2 + s3_len == len(Str)): return True # otherwise call recursively for n2, s3 return checkSumStrUtil(Str, beg + len1, len2,s3_len) # we do not get s3 in main string return False# Returns True if str is sum string, else False.def isSumStr(Str): n = len(Str) # choosing first two numbers and checking # whether it is sum-string or not. for i in range(1,n): for j in range(1,n-i): if (checkSumStrUtil(Str, 0, i, j)): return True return False# Driver codeprint(isSumStr("1212243660"))print(isSumStr("123456787"))# This code is contributed by shinjanpatra |
Javascript
<script>// JavaScript code to implement the approach// this is function for finding sum of two// numbers as stringfunction string_sum(str1, str2){ if (str1.length < str2.length){ let temp = str1 str1 = str2 str2 = temp } let m = str1.length let n = str2.length let ans = "" // sum the str2 with str1 let carry = 0 for(let i=0;i<n;i++){ // Sum of current digits let ds = ((str1.charCodeAt(m - 1 - i) - '0'.charCodeAt(0)) + (str2.charCodeAt(n - 1 - i) - '0'.charCodeAt(0)) + carry) % 10 carry = Math.floor(((str1.charCodeAt(m - 1 - i) - '0'.charCodeAt(0)) + (str2.charCodeAt(n - 1 - i) - '0'.charCodeAt(0)) + carry) / 10) ans = ds.toString() + ans } for(let i=n;i<m;i++){ let ds = ((str1.charCodeAt(m - 1 - i) - '0'.charCodeAt(0)) + (str2.charCodeAt(n - 1 - i) - '0'.charCodeAt(0)) + carry) % 10 carry = Math.floor(((str1.charCodeAt(m - 1 - i) - '0'.charCodeAt(0)) + (str2.charCodeAt(n - 1 - i) - '0'.charCodeAt(0)) + carry) / 10) ans = ds.toString() + ans } if (carry) ans = carry.toString() + ans return ans}// Returns true of two substrings of given// lengths of str[beg..] can cause a positive// result.function checkSumStrUtil(Str, beg,len1, len2){ // Finding two substrings of given lengths // and their sum let s1 = Str.substring(beg,beg+len1) let s2 = Str.substring(beg + len1, beg + len1 +len2) let s3 = string_sum(s1, s2) let s3_len = s3.length // if number of digits s3 is greater then // the available string size if (s3_len > Str.length - len1 - len2 - beg) return false // we got s3 as next number in main string if (s3 == Str.substring(beg + len1 + len2, beg + len1 + len2 +s3_len)){ // if we reach at the end of the string if (beg + len1 + len2 + s3_len == Str.length) return true // otherwise call recursively for n2, s3 return checkSumStrUtil(Str, beg + len1, len2,s3_len) } // we do not get s3 in main string return false}// Returns true if str is sum string, else false.function isSumStr(Str){ let n = Str.length // choosing first two numbers and checking // whether it is sum-string or not. for(let i=1;i<n;i++){ for(let j=1;j<n-i;j++){ if (checkSumStrUtil(Str, 0, i, j)) return true } } return false}// Driver codedocument.write(isSumStr("1212243660"))document.write(isSumStr("123456787"))// This code is contributed by shinjanpatra</script> |
Output:
1 0
Time Complexity: O(n*n*n), where n is the length of string.
Auxiliary Space: O(n), where n is the length of string.
This article is contributed by Jay Prakash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




.png)