An additive number is a string whose digits can form an additive sequence.
A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.
Given a string containing only digits, return true
if it is an additive number or false
otherwise.
Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03
or 1, 02, 3
is invalid.
Example 1:
Input: "112358" Output: true Explanation: The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
Example 2:
Input: "199100199" Output: true Explanation: The additive sequence is: 1, 99, 100, 199. 1 + 99 = 100, 99 + 100 = 199
Constraints:
1 <= num.length <= 35
num
consists only of digits.
Follow up: How would you handle overflow for very large input integers?
This problem asks whether a given string num
can be partitioned into at least three numbers forming an additive sequence. An additive sequence means that each number (after the first two) is the sum of the previous two. Numbers cannot have leading zeros.
The solution employs a depth-first search (DFS) approach, which is a type of backtracking. It explores all possible partitions of the input string to check if they satisfy the additive sequence condition.
Iteration through possible first two numbers: The outer loops iterate through all possible lengths for the first two numbers in the sequence. i
represents the length of the first number, and j
represents the length of the second number. The loops ensure that these lengths are within bounds and handle the case where numbers can't have leading zeros.
Parsing first two numbers: The first two numbers (a
and b
) are parsed from the input string based on the lengths i
and j - i
.
Recursive DFS (dfs
function): The dfs
function recursively checks if the rest of the string forms an additive sequence based on the current a
and b
.
num
is empty), it means a valid additive sequence has been found, so true
is returned.a + b
is greater than 0 and the first digit of the remaining string is '0', it's an invalid sequence (leading zeros are not allowed), so false
is returned.a + b
, the dfs
function is recursively called with the updated a
and b
(the second and third numbers become the new first and second).true
, it means a valid additive sequence has been found. Otherwise, the loop continues to try different lengths for the next number. If no valid next number is found, it returns false
.Return value: The main function returns true
if a valid additive sequence is found during the iteration; otherwise, it returns false
.
Time Complexity: O(N^3), where N is the length of the input string. The nested loops have O(N^2) complexity. The recursive dfs
function also iterates, in the worst case, through all the remaining characters in the string, adding another O(N) factor, resulting in the overall O(N^3) complexity.
Space Complexity: O(N) in the worst case. This is due to the recursive calls in dfs
, which can have a maximum depth of N in the worst case (a sequence where each number is a single digit). The space used is mainly for the call stack during recursion.
The provided Java and C++ solutions include a min(..., 19)
constraint within their loops. This helps prevent stack overflow issues for extremely long input strings. This limitation means that the solution might miss some valid additive sequences if those sequences require numbers with more than 18 digits. Without this precaution, it is possible to hit stack overflow errors during recursion.
The Python code is slightly more concise because it implicitly handles potential integer overflow using Python's arbitrary-precision integers. The Java and C++ code uses long
which still has a limitation of integer size. To handle potentially larger numbers, you would need to use a Big Integer library.