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 email@example.com. 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.
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be repeated to make another string
- 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 a string follows a^nb^n pattern or not
- Check if a string is the typed name of the given name
- Check whether a given string is Heterogram or not
- Check if a string has m consecutive 1's or 0's
- Check if a string is suffix of another
- Check if a string is Isogram or not
- Check if a string is substring of another
- Check if it is possible to transform one string to another
- Check if any anagram of a string is palindrome or not