Jack Jack - 1 month ago 5
Java Question

How to show all combinations of IP address that can be created from a string of numbers?

I need to format the input string into IP address format, so I have the following code;however,the numbers are fixed and I am not sure how to generated different values for a single input.

Other constraints would be to make sure no group of numbers is more than 255, but in this case I just want to put them in four separate groups and each group must have 1 to 3 members.

Vimal's question: From provided string 19216801, I think you cant identify exact ip. It can be 192.168.0.1 or 19.216.80.1 or any other combination.

Answer: I am not looking for any specific IP I just need to show all the possible combinations.

Sample formats

Some of the combinations would be as following

Expected result | number of input characters

1.1.1.1 4
....
1.1.1.2 5
1.1.2.1
1.2.1.1
2.1.1.1
....
1.1.1.3 6
1.1.3.1
1.3.1.1
3.1.1.1
....
2.2.2.1 7
2.2.1.2
....
2.2.2.2 8
3.2.2.1
1.2.2.3
....
2.2.2.3 9
3.3.2.1
1.2.3.3
....
3.3.3.1 10
3.3.1.3
3.1.3.3
1.3.3.3
....
3.3.3.2 11
3.3.2.3
3.2.3.3
....
3.3.3.3 12


Code

String number = "19216801";
if (number.length() == 4) {
StringBuilder sb = new StringBuilder(number)
.insert(1, ".")
.insert(1, ".")
.insert(1, ".")
.insert(1, ".");
String output = sb.toString();
System.out.println(output);
}
if (number.length() == 8) {
StringBuilder sb = new StringBuilder(number)
.insert(2, ".")
.insert(2, ".")
.insert(2, ".")
.insert(2, ".");
String output = sb.toString();
System.out.println(output);
}
if (number.length() == 12) {
StringBuilder sb = new StringBuilder(number)
.insert(3, ".")
.insert(3, ".")
.insert(3, ".")
.insert(3, ".");
String output = sb.toString();
System.out.println(output);
}

Answer

Rephrase task in next way.

  1. imagine that ip part can have zero digit so ... is valid
  2. then we have number.length() - 3 elements and need to put 3 dot in any position
  3. let a, b, c be length of part
  4. first part can be any length for(int a = 0; a < l; a++)
  5. second one must be shorter for(int b = 0; b < l-a; b++)
  6. same with third, total length must be l. so l>=a+b+c is constraint. c
  7. put points in it places.
  8. first poin just after first part (don't forget tat at first step we cut one digit from each part).
  9. second is after first part, first dot and second part ((a +1) + 1 + (b+1))
  10. third one the same. skip first part (a+1), dot (+1), second part (+b+1), second dot (+1) and third part (c+1) = a+b+c+5

    String number = "19216801";
    int l = number.length() - 3;
    for(int a = 0; a < l; a++) {
        for(int b = 0; b < l-a; b++){
            for(int c = 0; c <l-a-b; c++){
                StringBuilder sb = new StringBuilder(number);
                sb.insert(a+1, ".");
                sb.insert(a+b+3, ".");
                sb.insert(a+b+c+5, ".");
                System.out.println(sb);
            }
        }
    }
    

It pretty difficult to explain, most of code come from background of my mind i just write it.