Program to generate all possible valid IP addresses from given string | Set 2

20 May, 2022
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.35Input: 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.
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++
Java
Python3
Javascript
// C++ implementation of the approach#include <iostream>#include <vector>using namespace std; // Function to get all the valid ip-addressesvoid 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 codeint 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";    }}
/*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 implementation of the approach # Function to get all the valid ip-addressesdef 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 codeif __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
<script> // JavaScript implementation of the approach // Function to get all the valid ip-addressesfunction 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-addresseslet result = [] ;GetAllValidIpAddress(result, givenString, 0, 0, []); // Print all the generated ip-addressesfor(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 constantAuxiliary Space: O(1)
ankthon
pankajsharmagfg
surinderdawra388
shinjanpatra
Algorithms
Backtracking
Strings
Strings
Backtracking
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
CPU Scheduling in Operating Systems
What is Hashing | A Complete Tutorial
Understanding Time Complexity with Simple Examples
Write a program to print all permutations of a given string
Hamiltonian Cycle | Backtracking-6
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Sudoku | Backtracking-7
Backtracking | Introduction