Here’s my explanation for the leetcode 306: Additive Number problem
Additive Number Problem
Problem Recap
An additive number is a string of digits where the sequence of numbers formed by splitting the string satisfies the condition that each number (after the first two) is the sum of the two preceding numbers.
Examples:
"112358"
is additive because the sequence is1, 1, 2, 3, 5, 8
, and:1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
"199100199"
is additive because the sequence is1, 99, 100, 199
, and:1 + 99 = 100
99 + 100 = 199
Solution Approach
The solution uses a backtracking approach to try all possible splits of the string into sequences of numbers and checks if any of these sequences form an additive sequence.
Detailed Explanation
Helper Function isValid
This recursive function checks if the remaining part of the string s
forms a valid additive sequence given the first two numbers a
and b
.
Base Case: If the remaining string
s
is empty, it means we’ve successfully formed an additive sequence, so returntrue
.Recursive Step:
- Compute the sum of
a
andb
and convert it to a stringsum
. - Check if the remaining string
s
starts withsum
:- If not, the sequence is invalid → return
false
. - If yes, recursively check the next part of the string with the new pair
(b, sum)
and the remaining string after removingsum
.
- If not, the sequence is invalid → return
- Compute the sum of
Main Function isAdditiveNumber
- Initialization: Get the length of the input string
num
. - Nested Loops:
- The outer loop (
i
) determines the end index of the first numbera
(from index0
toi
). - The inner loop (
j
) determines the end index of the second numberb
(from indexi
toj
).
- The outer loop (
- Leading Zero Check:
- Skip any splits where
a
orb
have leading zeros unless they are exactly"0"
.- Example:
"02"
is invalid, but"0"
is valid.
- Example:
- Skip any splits where
- Validation:
- For each valid pair
(a, b)
, callisValid
to check if the remaining part of the string forms a valid additive sequence starting witha
andb
. - If
isValid
returnstrue
, immediately returntrue
from the main function.
- For each valid pair
- Final Check:
- If no valid sequence is found after all possible splits, return
false
.
- If no valid sequence is found after all possible splits, return
Example Walkthrough
Let’s walk through the example num = "112358"
:
- First Iteration (
i = 1
,j = 2
):a = "1"
,b = "1"
.- No leading zeros → proceed.
isValid(1, 1, "2358")
:- Sum of
1 + 1 = 2
. - Check if
"2358"
starts with"2"
→ Yes. - Recursively call
isValid(1, 2, "358")
:- Sum of
1 + 2 = 3
. - Check if
"358"
starts with"3"
→ Yes. - Recursively call
isValid(2, 3, "58")
:- Sum of
2 + 3 = 5
. - Check if
"58"
starts with"5"
→ Yes. - Recursively call
isValid(3, 5, "8")
:- Sum of
3 + 5 = 8
. - Check if
"8"
starts with"8"
→ Yes. - Recursively call
isValid(5, 8, "")
:- Empty string → return
true
.
- Empty string → return
- Sum of
- Sum of
- Sum of
- Sum of
- Since
isValid
returnedtrue
, the main function returnstrue
.
Edge Cases
- Leading Zeros: Correctly skips invalid splits (e.g.,
"02"
unless it’s"0"
). - Single Digit: If input length < 3, returns
false
. - Large Numbers: Uses
parseInt
, butBigInt
is better for very large numbers to avoid precision issues.
Time Complexity
- Nested loops: O(n²), where
n
is the string length. isValid
function: O(n) per pair(a, b)
.- Overall: O(n³), feasible for reasonably sized strings.
Space Complexity
- O(n) due to recursion stack in the worst case.
Code
|
|
|
|