Watch 10 video solutions for Additive Number, a medium level problem involving String, Backtracking. This walkthrough by NeetCode has 186,659 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 35num consists only of digits.
Follow up: How would you handle overflow for very large input integers?
Problem Overview: You are given a numeric string num. The task is to determine whether it can form an additive sequence. In an additive sequence, every number (starting from the third) equals the sum of the previous two. Numbers cannot have leading zeros unless the number itself is 0.
Approach 1: Recursive Backtracking with Early Termination (O(n^3) time, O(n) space)
This approach tries all possible ways to split the first two numbers in the string, then recursively verifies whether the remaining string follows the additive rule. Start by choosing indices i and j to define the first two numbers. Convert them to integers (or use string-based addition to avoid overflow), compute their sum, and check if the next part of the string begins with that value. If it matches, continue the process recursively with the new pair. Early termination happens when a generated sum does not match the next substring, which prunes large parts of the search tree. The method naturally fits a backtracking pattern because you explore candidate splits and abandon invalid sequences quickly. Parsing and substring comparisons lead to O(n^3) time in the worst case, while recursion depth and substring storage require O(n) space.
Approach 2: Iterative Approach with Two Pointers (O(n^3) time, O(1) space)
This approach removes recursion and checks sequences iteratively. First, iterate over all possible splits for the first and second numbers using two pointers. After fixing these two values, simulate the additive sequence by repeatedly computing their sum and checking if the next substring matches it. If it matches, move the pointers forward and continue with the new pair. If any mismatch occurs, break early and try another split. Since you only track a few indices and temporary values, the extra space stays at O(1). However, generating candidate pairs and validating the remaining substring still leads to O(n^3) time complexity in the worst case. The approach mainly relies on careful string manipulation and controlled pointer movement similar to patterns used in two pointers problems.
Recommended for interviews: The recursive backtracking solution is usually preferred in interviews because it clearly expresses the search space and shows that you can prune invalid paths early. Interviewers expect you to handle leading zeros, large number addition, and substring validation carefully. The iterative version demonstrates strong control over pointer logic and space optimization. Showing the brute-force split idea first, then refining it with early termination, signals strong problem-solving skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking with Early Termination | O(n^3) | O(n) | Best for interviews when demonstrating recursive exploration and pruning invalid sequences |
| Iterative Two-Pointer Validation | O(n^3) | O(1) | Useful when avoiding recursion or when memory usage must stay minimal |