public boolean isAdditiveNumber(String num) {
        if (num == null) return false;

        for (int i = 1; i <= num.length() / 2; ++i) { // optimize for i < num.length()
            for (int j = i + 1; Math.max(i, j - i) <= num.length() - j; ++j) { // optimize for j < num.length()
                if (isValid(0, i, j, num)) return true;
            }
        }
        return false;
    }