07/17/2016 Depth First Search Math String Dynamic Programming

## Question

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.

How would you handle overflow for very large input integers?

## Solution

Result: Accepted Time: 0 ms

Here should be some explanations.

bool plus_equal(char * num, int i, int j, int k)
{
int carry = 0, a = i, b = j, c = k;
if((num == '0'&& i > 1)  || (num[i]== '0' && j  > i+1) || (num[j] == '0'&& k > j+1))
return false;
while(a > 0 || b > i || carry)
{
if(c <= j) return false;
if(a > 0)
carry += num[--a] - '0';
if(b > i)
carry += num[--b] - '0';
if(carry%10 != num[--c] - '0')
return false;
carry /= 10;
}
return c == j;
}
bool dfs(char *num,int i,int j,int len)
{
if(j >= len)
return true;
int k = i+j;
if(k < j*2 - i)
k = j*2 - i;
if(k <=len && plus_equal(num,i,j,k) && dfs(num+i,j-i,k-i,len - i))
return true;
if(k < len && plus_equal(num,i,j,k+1) && dfs(num+i,j-i,k+1-i,len - i))
return true;
return false;
}


• Time Complexity: $O(n^2)$
• Space Complexity: $O(1)$