public boolean isAdditiveNumber(String num){
  int n=num.length();
  for (int i=1; i <= n / 2; i++) {
    if (num.charAt(0) == '0' && i > 1) {
      return false;
    }
    BigInteger num1=new BigInteger(num.substring(0,i));
    for (int j=1; Math.max(i,j) <= n - i - j; j++) {
      if (num.charAt(i) == '0' && j > 1) {
        break;
      }
      BigInteger num2=new BigInteger(num.substring(i,i + j));
      if (isAdditiveNumber(num1,num2,i + j,num)) {
        return true;
      }
    }
  }
  return false;
}

public boolean isAdditiveNumber ( String num ) { int n = num . length ( ) ; for ( int i = 1 ; i <= n / 2 ; i ++ ) { if ( num . charAt ( 0 ) == '0' && i > 1 ) { return false ; } BigInteger num1 = new BigInteger ( num . substring ( 0 , i ) ) ; for ( int j = 1 ; Math . max ( i , j ) <= n - i - j ; j ++ ) { if ( num . charAt ( i ) == '0' && j > 1 ) { break ; } BigInteger num2 = new BigInteger ( num . substring ( i , i + j ) ) ; if ( isAdditiveNumber ( num1 , num2 , i + j , num ) ) { return true ; } } } return false ; }
recursive . generate the first and second of the sequence , check if the rest of the string match the sum recursively . i is the length of first number , j is the length of thgste second .
