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: str = “25525511135”
Input: str = “11111011111”
Approach: This problem can be solved using backtracking. In each call we have three options to create a single block of numbers of a valid ip address:
- Either select only a single digit, add a dot and move onto selecting other blocks (further function calls).
- Or select two digits at the same time, add a dot and move further.
- Or select three consecutive digits and move for the next block.
At the end of the fourth block, if all the digits have been used and the address generated is a valid ip-address then add it to the results and then backtrack by removing the digits selected in the previous call.
Below is the implementation of the above approach:
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
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- 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
- 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 | SET 2 (Regular Expression approach)
- 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) in Java
- Check whether the given string is a valid identifier
- Generate a String from given Strings P and Q based on the given conditions
- Check if the given permutation is a valid BFS of a given Tree
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Iterative program to generate distinct Permutations of a String
- Program to check if a date is valid or not
- Program to check for a Valid IMEI Number
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 : AnkitRai01