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?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for determining if a number is an additive number involves trying all possible starting numbers within the given string. We exhaustively check if the remaining digits can be formed by adding the previous two numbers. If any combination satisfies the additive property across the whole input number, we consider it a valid additive number.
Here's how the algorithm would work step-by-step:
def is_additive_number(number_string):
string_length = len(number_string)
for first_number_length in range(1, (string_length // 2) + 1):
for second_number_length in range(1, (string_length - first_number_length) // 2 + 1):
first_number = number_string[:first_number_length]
second_number = number_string[first_number_length:first_number_length + second_number_length]
# Handle leading zeros; if so, this combination is invalid
if (len(first_number) > 1 and first_number[0] == '0') or (len(second_number) > 1 and second_number[0] == '0'):
continue
remaining_string = number_string[first_number_length + second_number_length:]
first_number_int = int(first_number)
second_number_int = int(second_number)
while remaining_string:
sum_numbers = str(first_number_int + second_number_int)
# Need to check if sum is a prefix of the remaining string
if not remaining_string.startswith(sum_numbers):
break
remaining_string = remaining_string[len(sum_numbers):]
first_number_int = second_number_int
second_number_int = int(sum_numbers)
# If we've exhausted the string, then it's an additive number
if not remaining_string:
return True
return False
To determine if a number sequence is additive, we aim to efficiently verify if later parts of the sequence can be formed by adding earlier parts. This is done by intelligently testing possibilities to reduce unnecessary calculations. Instead of exhaustively checking every possible breakdown of the string, we use a directed approach to validate potential additive sequences.
Here's how the algorithm would work step-by-step:
def isAdditiveNumber(number_string):
string_length = len(number_string)
for first_number_length in range(1, (string_length // 2) + 1):
for second_number_length in range(1, (string_length - first_number_length) // 2 + 1 if string_length - first_number_length > 1 else string_length - first_number_length + 1):
first_number = int(number_string[:first_number_length])
second_number = int(number_string[first_number_length:first_number_length + second_number_length])
remaining_string = number_string[first_number_length + second_number_length:]
# Core logic: check if the remaining string forms an additive sequence.
while remaining_string:
expected_sum = str(first_number + second_number)
if not remaining_string.startswith(expected_sum):
break
remaining_string = remaining_string[len(expected_sum):]
first_number = second_number
second_number = int(expected_sum)
# If remaining_string is empty, the entire string forms an additive sequence.
if not remaining_string:
return True
return False
Case | How to Handle |
---|---|
Empty string input | Return false immediately as an empty string cannot form an additive sequence. |
String with length less than 3 | Return false because an additive sequence requires at least three numbers. |
Leading zeros in any number segment of the sequence | Reject sequences where numbers have leading zeros, except for zero itself. |
Integer overflow when summing large numbers | Use a data type like `long` or handle large number addition using string manipulation to avoid overflow. |
Very long input string causing performance issues | Implement backtracking with pruning to reduce the search space and avoid exponential time complexity. |
Input string containing non-numeric characters | Return false if the string contains characters other than digits to prevent parsing errors. |
Additive sequence starts with zero | Handle the cases where the sequence begins with '0' correctly, remembering that numbers other than '0' cannot start with leading zeros. |
No valid additive sequence exists within the input string | Return false after exhaustively searching all possible partitions of the string and finding no valid sequence. |