Program to generate all possible valid IP addresses from given string | Set 2
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.
Examples:
Input: str = “25525511135”
Output:
255.255.11.135
255.255.111.35
Input: str = “11111011111”
Output:
111.110.11.111
111.110.111.11
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:
C++
// C++ implementation of the approach #include <iostream> #include <vector> using namespace std; // Function to get all the valid ip-addresses void GetAllValidIpAddress(vector<string>& result, string givenString, int index, int count, string ipAddress) { // If index greater than givenString size // and we have four block if (givenString.size() == index && count == 4) { // Remove the last dot ipAddress.pop_back(); // Add ip-address to the results result.push_back(ipAddress); return ; } // To add one index to ip-address if (givenString.size() < index + 1) return ; // Select one digit and call the // same function for other blocks ipAddress = ipAddress + givenString.substr(index, 1) + '.' ; GetAllValidIpAddress(result, givenString, index + 1, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove two index (one for the digit // and other for the dot) from the end ipAddress.erase(ipAddress.end() - 2, ipAddress.end()); // Select two consecutive digits and call // the same function for other blocks if (givenString.size() < index + 2 || givenString[index] == '0' ) return ; ipAddress = ipAddress + givenString.substr(index, 2) + '.' ; GetAllValidIpAddress(result, givenString, index + 2, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove three index from the end ipAddress.erase(ipAddress.end() - 3, ipAddress.end()); // Select three consecutive digits and call // the same function for other blocks if (givenString.size() < index + 3 || stoi(givenString.substr(index, 3)) > 255) return ; ipAddress += givenString.substr(index, 3) + '.' ; GetAllValidIpAddress(result, givenString, index + 3, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove four index from the end ipAddress.erase(ipAddress.end() - 4, ipAddress.end()); } // Driver code int main() { string givenString = "25525511135" ; // Fill result vector with all valid ip-addresses vector<string> result; GetAllValidIpAddress(result, givenString, 0, 0, "" ); // Print all the generated ip-addresses for ( int i = 0; i < result.size(); i++) { cout << result[i] << "\n" ; } } |
Java
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; class GFG { // Function to get all the valid ip-addresses static void GetAllValidIpAddress(ArrayList<String>result, String givenString, int index, int count, String ipAddress){ // If index greater than givenString size // and we have four block if (givenString.length() == index && count == 4 ){ // Remove the last dot ipAddress = ipAddress.substring( 0 ,ipAddress.length()- 1 ); // Add ip-address to the results result.add(ipAddress); return ; } // To add one index to ip-address if (givenString.length() < index + 1 ) return ; // Select one digit and call the // same function for other blocks ipAddress = (ipAddress + givenString.substring(index , index + 1 ) + '.' ); GetAllValidIpAddress(result, givenString, index + 1 , count + 1 , ipAddress); // Backtrack to generate another possible ip address // So we remove two index (one for the digit // and other for the dot) from the end ipAddress = ipAddress.substring( 0 ,ipAddress.length() - 2 ); // Select two consecutive digits and call // the same function for other blocks if (givenString.length() < index + 2 || givenString.charAt(index) == '0' ) return ; ipAddress = ipAddress + givenString.substring(index,index + 2 ) + '.' ; GetAllValidIpAddress(result, givenString, index + 2 , count + 1 , ipAddress); // Backtrack to generate another possible ip address // So we remove three index from the end ipAddress = ipAddress.substring( 0 ,ipAddress.length() - 3 ); // Select three consecutive digits and call // the same function for other blocks if (givenString.length() < index + 3 || Integer.valueOf(givenString.substring(index,index + 3 )) > 255 ) return ; ipAddress += givenString.substring(index,index + 3 ) + '.' ; GetAllValidIpAddress(result, givenString, index + 3 , count + 1 , ipAddress); // Backtrack to generate another possible ip address // So we remove four index from the end ipAddress = ipAddress.substring( 0 ,ipAddress.length()- 4 ); } public static void main (String[] args) { String givenString = "25525511135" ; // Fill result vector with all valid ip-addresses ArrayList<String>result = new ArrayList<String>() ; String ipAddress = "" ; GetAllValidIpAddress(result, givenString, 0 , 0 ,ipAddress); // Print all the generated ip-addresses for ( int i = 0 ; i < result.size(); i++) System.out.println(result.get(i)); } } // This code is contributed by shinjanpatra. |
Python3
# Python3 implementation of the approach # Function to get all the valid ip-addresses def GetAllValidIpAddress(result, givenString, index, count, ipAddress) : # If index greater than givenString size # and we have four block if ( len (givenString) = = index and count = = 4 ) : # Remove the last dot ipAddress.pop(); # Add ip-address to the results result.append(ipAddress); return ; # To add one index to ip-address if ( len (givenString) < index + 1 ) : return ; # Select one digit and call the # same function for other blocks ipAddress = (ipAddress + givenString[index : index + 1 ] + [ '.' ]); GetAllValidIpAddress(result, givenString, index + 1 , count + 1 , ipAddress); # Backtrack to generate another possible ip address # So we remove two index (one for the digit # and other for the dot) from the end ipAddress = ipAddress[: - 2 ]; # Select two consecutive digits and call # the same function for other blocks if ( len (givenString) < index + 2 or givenString[index] = = '0' ) : return ; ipAddress = ipAddress + givenString[index:index + 2 ] + [ '.' ]; GetAllValidIpAddress(result, givenString, index + 2 , count + 1 , ipAddress); # Backtrack to generate another possible ip address # So we remove three index from the end ipAddress = ipAddress[: - 3 ]; # Select three consecutive digits and call # the same function for other blocks if ( len (givenString)< index + 3 or int ("".join(givenString[index:index + 3 ])) > 255 ) : return ; ipAddress + = givenString[index:index + 3 ] + [ '.' ]; GetAllValidIpAddress(result, givenString, index + 3 , count + 1 , ipAddress); # Backtrack to generate another possible ip address # So we remove four index from the end ipAddress = ipAddress[: - 4 ]; # Driver code if __name__ = = "__main__" : givenString = list ( "25525511135" ); # Fill result vector with all valid ip-addresses result = [] ; GetAllValidIpAddress(result, givenString, 0 , 0 , []); # Print all the generated ip-addresses for i in range ( len (result)) : print ("".join(result[i])); # This code is contributed by Ankitrai01 |
C#
// C# implementation of the above approach. using System; using System.Collections.Generic; public class GFG { // Function to get all the valid ip-addresses static void GetAllValidIpAddress(List< string > result, string givenString, int index, int count, string ipAddress) { // If index greater than givenString size // and we have four block if (givenString.Length == index && count == 4) { // Remove the last dot ipAddress = ipAddress.Remove(ipAddress.Length - 1, 1); // Add ip-address to the results result.Add(ipAddress); return ; } // To add one index to ip-address if (givenString.Length < index + 1) { return ; } // Select one digit and call the // same function for other blocks ipAddress = ipAddress + givenString.Substring(index, 1) + '.' ; GetAllValidIpAddress(result, givenString, index + 1, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove two index (one for the digit // and other for the dot) from the end ipAddress = ipAddress.Remove(ipAddress.Length - 2, 2); // Select two consecutive digits and call // the same function for other blocks if (givenString.Length < index + 2 || givenString[index] == '0' ) { return ; } ipAddress = ipAddress + givenString.Substring(index, 2) + '.' ; GetAllValidIpAddress(result, givenString, index + 2, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove three index from the end ipAddress = ipAddress.Remove(ipAddress.Length - 3, 3); // Select three consecutive digits and call // the same function for other blocks if (givenString.Length < index + 3 || Int64.Parse(givenString.Substring(index, 3)) > 255) { return ; } ipAddress = ipAddress + givenString.Substring(index, 3) + '.' ; GetAllValidIpAddress(result, givenString, index + 3, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove four index from the end ipAddress = ipAddress.Remove(ipAddress.Length - 4, 4); } static public void Main() { string givenString = "25525511135" ; // Fill result vector with all valid ip-addresses List< string > result = new List< string >(); GetAllValidIpAddress(result, givenString, 0, 0, "" ); // Print all the generated ip-addresses for ( int i = 0; i < result.Count; i++) { System.Console.WriteLine(result[i]); } } } // The code is contributed by Gautam goel. |
Javascript
<script> // JavaScript implementation of the approach // Function to get all the valid ip-addresses function GetAllValidIpAddress(result, givenString, index, count, ipAddress){ // If index greater than givenString size // and we have four block if (givenString.length == index && count == 4){ // Remove the last dot ipAddress = ipAddress.substring(0,ipAddress.length-1); // Add ip-address to the results result.push(ipAddress); return ; } // To add one index to ip-address if (givenString.length < index + 1) return ; // Select one digit and call the // same function for other blocks ipAddress = (ipAddress + givenString.substring(index , index + 1) + '.' ); GetAllValidIpAddress(result, givenString, index + 1, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove two index (one for the digit // and other for the dot) from the end ipAddress = ipAddress.substring(0,ipAddress.length - 2); // Select two consecutive digits and call // the same function for other blocks if (givenString.length < index + 2 || givenString[index] == '0' ) return ; ipAddress = ipAddress + givenString.substring(index,index + 2) + '.' ; GetAllValidIpAddress(result, givenString, index + 2, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove three index from the end ipAddress = ipAddress.substring(0,ipAddress.length - 3); // Select three consecutive digits and call // the same function for other blocks if (givenString.length < index + 3 || parseInt(givenString.substring(index,index + 3)) > 255) return ; ipAddress += givenString.substring(index,index + 3) + [ '.' ]; GetAllValidIpAddress(result, givenString, index + 3, count + 1, ipAddress); // Backtrack to generate another possible ip address // So we remove four index from the end ipAddress = ipAddress.substring(0,ipAddress.length-4); } // Driver code let givenString = "25525511135" ; // Fill result vector with all valid ip-addresses let result = [] ; GetAllValidIpAddress(result, givenString, 0, 0, []); // Print all the generated ip-addresses for (let i=0;i<result.length;i++) document.write(result[i], "</br>" ); // This code is contributed by shinjanpatra </script> |
255.255.11.135 255.255.111.35
Time Complexity: O(1), Since the total number of IP addresses are constant
Auxiliary Space: O(1)
Please Login to comment...