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. 184.108.40.206 is not valid either as you are not allowed to change the string. 250.11.255.255 is valid.
Approach: Split the string with ‘ . ‘ and then check for all corner cases. Before entering the loop, check the size of the 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 the empty list.
Below is the implementation of the above approach:
- Time Complexity: O(n^3), where n is the length of the string
Three nested traversal of the string is needed, where n is always less than 12.
- Auxiliary Space: O(n).
As as extra space is needed.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to generate all possible valid IP addresses from given string | Set 2
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Extracting email addresses using regular expressions in Python
- Working with IP Addresses in Python
- Print all valid words that are possible using Characters of Array
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Generate all possible permutations of a Number divisible by N
- Generate a string with maximum possible alphabets with odd frequencies
- Generate all rotations of a given string
- Generate a string which differs by only a single character from all given strings
- Generate all permutations of a string that follow given constraints
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- 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) in Java | SET 2 (Regular Expression approach)
- Check whether the given string is a valid identifier
- Generate a String from given Strings P and Q based on the given conditions
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Check if a + b = c is valid after removing all zeroes from a, b and c
- Python Program to Generate Random binary string
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.