Additive Number - Problem
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.
Input & Output
Example 1 — Valid Additive Number
$
Input:
num = "112358"
›
Output:
true
💡 Note:
The digits can form additive sequence: 1, 1, 2, 3, 5, 8. 1+1=2, 1+2=3, 2+3=5, 3+5=8
Example 2 — Invalid Sequence
$
Input:
num = "199100199"
›
Output:
true
💡 Note:
The additive sequence is: 1, 99, 100, 199. 1+99=100, 99+100=199
Example 3 — No Valid Sequence
$
Input:
num = "1023"
›
Output:
false
💡 Note:
Cannot form a valid additive sequence without leading zeros
Constraints
- 1 ≤ num.length ≤ 35
- num consists only of digits
Visualization
Tap to expand
Understanding the Visualization
1
Input
String of digits "112358"
2
Process
Try different ways to split into additive sequence
3
Output
Return true if valid sequence exists
Key Takeaway
🎯 Key Insight: Once you pick the first two numbers, the entire additive sequence is uniquely determined
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code