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. 220.127.116.11 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program to generate all possible valid IP addresses from given string | Set 2
- Python Program to Generate Random binary string
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Python - Kth Valid String
- Check whether the given string is a valid identifier
- Python | Check if string is a valid identifier
- How to check if a string is a valid keyword in Python?
- Python | Check whether a string is valid json or not
- Program to check for a Valid IMEI Number
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Generate all rotations of a given string
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Generate random String of given size in Java
- Generate all permutations of a string that follow given constraints
- Python | Generate random string of given length
- Generate a string with maximum possible alphabets with odd frequencies
- Python | Program to generate one-time password (OTP)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
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.