-
Notifications
You must be signed in to change notification settings - Fork 0
/
Additive Number.cpp
32 lines (28 loc) · 973 Bytes
/
Additive Number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
using namespace std;
class Solution {
public:
bool isAdditiveNumber(string num)
{
for(int i = 1; i < num.size(); i++)
{
string s1 = num.substr(0, i);
if(s1.size() > 1 && s1[0] == 0) break;
for(int j = i + 1; j < num.size(); j++)
{
string s2 = num.substr(i, j - i);
if(s2.size() > 1 && s2[0] == 0) break;
if(helper(num.substr(j), s1, s2)) return true;
}
}
return false;
}
bool helper(string num, string num1, string num2)
{
if((num1.size() > 1 && num1[0] == '0') || (num2.size() > 1 && num2[0] == '0')) return false;
string sumStr = to_string(stol(num1) + stol(num2));
if(sumStr == num) return true;
if(sumStr.size() >= num.size() || num.substr(0, sumStr.size()) != sumStr) return false;
return helper(num.substr(sumStr.size()), num2, sumStr);
}
};