<p>An <strong>additive number</strong> is a string whose digits can form an <strong>additive sequence</strong>.</p>

<p>A valid <strong>additive sequence</strong> should contain <strong>at least</strong> three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.</p>

<p>Given a string containing only digits, return <code>true</code> if it is an <strong>additive number</strong> or <code>false</code> otherwise.</p>

<p><strong>Note:</strong> Numbers in the additive sequence <strong>cannot</strong> have leading zeros, so sequence <code>1, 2, 03</code> or <code>1, 02, 3</code> is invalid.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> &quot;112358&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> 
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
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> &quot;199100199&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> 
The additive sequence is: 1, 99, 100, 199.&nbsp;
1 + 99 = 100, 99 + 100 = 199
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num.length &lt;= 35</code></li>
	<li><code>num</code> consists only of digits.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> How would you handle overflow for very large input integers?</p>

class Solution {
public:
    bool isAdditiveNumber(string num) {
        for (int i = 1; i < num.length(); ++i) {
            for (int j = i + 1; j < num.length(); ++j) {
                string s1 = num.substr(0, i), s2 = num.substr(i, j - i);
                if ((s1.length() > 1 && s1[0] == '0') ||
                    (s2.length() > 1 && s2[0] == '0')) {
                    continue;
                }
                string next = add(s1, s2);
                string cur = s1 + s2 + next;
                while (cur.length() < num.length()) {
                    s1 = s2;
                    s2 = next;
                    next = add(s1, s2);
                    cur += next;
                }
                if (cur == num) {
                    return true;
                }
            }
        }
        return false;
    }
private:
        string add(const string& m, const string& n) {
            string res;
            int res_length = max(m.length(), n.length()) ;
    
            int carry = 0;
            for (int i = 0; i < res_length; ++i) {
                int m_digit_i = i < m.length() ? m[m.length() - 1 - i] - '0' : 0;
                int n_digit_i = i < n.length() ? n[n.length() - 1 - i] - '0' : 0;
                int sum = carry + m_digit_i + n_digit_i;
                carry = sum / 10;
                sum %= 10;
                res.push_back('0' + sum);
            }
            if (carry) {
                res.push_back('0' + carry);
            }
            reverse(res.begin(), res.end());
            return res;
        }
};
7900