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.
“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.
This article is contributed by Jay Prakash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a string can become empty by recursively deleting a given sub-string
- Check if left and right shift of any string results into given string
- Check if a given string is a Reverse Bitonic String or not
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if a string is concatenation of another given string
- Check if the given string is shuffled substring of another string
- Check whether given string can be generated after concatenating given strings
- Check if given Binary string follows then given condition or not
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Restore original String from given Encrypted String by the given operations
- Check if a string can be obtained by rotating another string 2 places
- String slicing in Python to check if a string can become empty by recursive deletion
- Check whether second string can be formed from characters of first string
- Check if a string contains a palindromic sub-string of even length
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string d places
- Check if any permutation of string is a K times repeated string