Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'nd substring.
Examples:
Input : "11235813"
Output : ["1", "1", "2", "3", "5", "8", "13"]
Input : "1111223"
Output : ["1", "11", "12", "23"]
Input : "1111213"
Output : ["11", "1", "12", "13"]
Input : "11121114"
Output : []
- Iterate through the given string by picking 3 numbers (first, seconds and third) at a time starting from one digit each.
- If first + second = third, recursively call check() with second as first and third as second. The third is picked based on next possible number of digits. (The result of addition of two numbers can have a max. of second's & third's digits + 1)
- Else, first increment the third (by adding more digits) till the limit (Here limit is sum of first and second).
- After incrementing third, following cases arise. a) When doesn't match, increment the second offset. b) When doesn't match, increment the first offset. c)
- Note: Once a call to check() is made after incrementing the third offset, do not alter the second or first, as those are already finalized.
- When the end of the string is reached and the condition is satisfied, add "second" and "third" to the empty list. While rolling back the recursive stack, prepend the "first" to the list so the order is preserved.
Implementation:
Output:true
[1, 1, 2, 3, 5, 8, 13]
Time Complexity: O(n)
Auxiliary Space: O(n)