Yu's Coding Garden
My blog for LeetCode Questions and Answers...
LeetCode Online Judge Questions Table
Leetcode (Finished)
leetCode Question: Additive Number
Additive Number
Additive number is a string whose digits can form 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.
For example:
"112358" is an additive number because 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
"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199
Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.
Analysis
For this question, the first thing we could handle is
- How to determine the sum of two strings in int is equal to the other string in int?
I use the string manipulation to handle the case when the number is too larger to be stored in an long long type (in cpp). This is implemented in the checkEQ function shown in my code below. Note that, the question is not allowed to use "03", "02", ect format, we also have to eliminte such cases (by the if condition in Line 6 below). In my implementation, the swap operation is to make the longer number always stores in a. The Line 5 is to ignore the check if the two numbers a and c are totally different, regarding to the fact that the sum of two numbers will not exceede the max length of two numbers + 1.
E.g., a = 998, b = 80, c = a + b = 998 + 80 = 1078
The length of c will never be greater than length of a + 1, since the carry will never greater than 1. Using this properties of sum operation, we could easily implement the following code for checking the sum of three strings as shown in checkEQ function. DO NOT forget to check the last carry!
Next we are facing the main part of the problem: find a way to check the whole string. I will show how I handle this in a clear way (which may not be optimal but quite easy to understand).
From the problem we know that:
- The length of each number could be different.
- At least we should have 3 numbers, which forms the whole string.
- If we find one match (a + b = c), we shall keep searching (b + c = ???)
- If our search goes to the end of the number, we could return True.
These observations tell us that:
- We could check each possible length of each number
- The max length of first number is length of string - 2, the max length of second number is length of string - length of 1st number - 1.
- We could use a recursion to do this search, since every time, the procedure is almost same, but the input data are different.
- The termination condition of our recursion is whether we match the whole string.
Given these infomation, we could implement our recursion function, which is shown in my code below:
Code (C++):
Code(Python):