An Additive Number is a string where the digits form an additive sequence. In such a sequence, every number (starting from the third) is the sum of the preceding two. For example, "112358" is additive because . The Additive Number coding problem asks you to determine if a given string is additive.
Companies like Meta and Google ask this to test your Backtracking and String parsing skills. Unlike some problems with fixed-length numbers, the numbers in an additive sequence can have any number of digits. You must explore various ways to split the string, which requires a recursive or iterative search strategy.
This is a classic Backtracking interview pattern. You pick the first two numbers (there are ways to do this), and then the rest of the sequence is strictly determined by those two. You recursively check if the next part of the string matches the sum of the previous two numbers.
Input: "199100199"
true.long or int to store the sum will fail. You must use string-based addition or BigInteger.Since the first two numbers determine the entire sequence, you don't need full backtracking for the whole string. Once you've picked the first two, the rest is a simple iterative check. This reduces the search space significantly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| The k-th Lexicographical String of All Happy Strings of Length n | Medium | Solve | |
| Split Array into Fibonacci Sequence | Medium | Solve | |
| Restore IP Addresses | Medium | Solve | |
| Ambiguous Coordinates | Medium | Solve | |
| Generalized Abbreviation | Medium | Solve |