![]() |
VOOZH | about |
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. A valid string should contain at least three digit to make one additive sequence.
Examples:
Input : s = ā235813ā
Output : true
Explanation: 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13Input : s = ā199100199ā
Output : true
Explanation: 1 + 99 = 100, 99 + 100 = 199Input : s = ā12345678ā
Output : false
Table of Content
The idea is to try all possible ways to split the string into the first two numbers, then check if the string after the two is sum of the two and recursively verify whether the rest of the string follows the additive sequence property.
true
The idea is to avoid deep recursion and instead simulate the additive sequence iteratively. We try all possible splits for the first two numbers and then keep generating the next number using string addition. At each step, we check whether the generated sum matches the corresponding substring in the original string. If the sequence continues till the end, it is valid.
true