Given a string containing only digits, restore it by returning all possible valid IP address combinations.
A valid IP address must be in the form of A.B.C.D, where A, B, C, and D are numbers from 0-255. The numbers cannot be 0 prefixed unless they are 0.
Input : 25525511135 Output : [“255.255.11.135”, “255.255.111.35”] Explanation: These are the only valid possible IP addresses. Input : "25505011535" Output :  Explanation : We cannot generate a valid IP address with this string.
First, we will place 3 dots in the given string and then Try out all the possible combinations for the 3 dots.
Corner case for validity :
For string "25011255255" 25.011.255.255 is not valid as 011 is not valid. 126.96.36.199 is not valid either as you are not allowed to change the string. 250.11.255.255 is valid.
Split the string with ‘ . ‘ and then check for all corner cases. Before entering the loop, check the size of string. Generate all the possible combinations using looping through the string. If IP is found to be valid then return the IP address, else simply return empty list.
Below is the implementation of above approach :
- Program to generate all possible valid IP addresses from given string | Set 2
- Check whether the given string is a valid identifier
- Python | Check whether a string is valid json or not
- Python | Check if string is a valid identifier
- How to check if a string is a valid keyword in Python?
- Program to check for a Valid IMEI Number
- Generate all rotations of a given string
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Python | Generate random string of given length
- Generate random String of given size in Java
- Generate all permutations of a string that follow given constraints
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi hooda