private static void restoreIpAddressesHelper(String s, int index, int size,
                          String[] solution, ArrayList<String> result) {
        if (index == s.length() && size == 4) {
            result.add(solution[0] + "." + solution[1] + "." + 
                   solution[2] + "." + solution[3]);
            return;
        }
        else if (index == s.length() || size == 4) {
            return;
        }
        
        /* try 1 digit */
        int num = Integer.parseInt(s.substring(index, index + 1));
        solution[size] = s.substring(index, index + 1);
        size++;
        restoreIpAddressesHelper(s, index + 1, size, solution, result);
        size--;

        if (num == 0) {
            return;
        }

        /* try 2 digits */
        if (index + 2 <= s.length()) {
            solution[size] = s.substring(index, index + 2);
            size++;
            restoreIpAddressesHelper(s, index + 2, size, solution, result);
            size--;
        }

        /* try 3 digits */
        if (index + 3 <= s.length()) {
            num = Integer.parseInt(s.substring(index, index + 3));
            if ( num >= 0 && num <= 255) {
                solution[size] = s.substring(index, index + 3);
                size++;
                restoreIpAddressesHelper(s, index + 3, size, solution, result);
                size--;
            }
        }
    }