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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Queries to check if string B exists as substring in string A
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string d places
- Check if a string can be obtained by rotating another string 2 places
- Check if any permutation of string is a K times repeated string
- Check if left and right shift of any string results into given string
- Check if a string is a scrambled form of another string
- Check whether second string can be formed from characters of first string
- Check if the given string is shuffled substring of another string
- Check if a string contains a palindromic sub-string of even length
- Check if a string can be repeated to make another string
- Check whether a given string is Heterogram or not
- Check if the given string is K-periodic
- Check if a string is Isogram or not
- Check if one string can be converted to another
- Check if a string is substring of another
- Check if it is possible to transform one string to another