Restore IP Addresses in C++


Suppose we have a string containing only digits, we have to restore it by returning all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single points.

So, if the input is like "25525511135", then the output will be ["255.255.11.135", "255.255.111.35"]

To solve this, we will follow these steps −

  • Define a function convertToNum(), this will take s, start, end,

  • num := 0

  • for initialize i := start, when i <= end, update (increase i by 1), do −

    • num := (num * 10) + (s[i] - ASCII of '0')

    • if num > 255, then −

      • return 10000

  • return num

  • Define a function addDots(), this will take positions,

  • res := blank string

  • x := 0, posIndex := 0

  • for initialize i := 0, when i < size of positions, update (increase i by 1), do −

    • num := positions[i]

    • create one string str1

    • temp := num as string

    • res := res + temp

    • if i < size of positions, then −

      • res := res concatenate "."

  • return res

  • Define a function solve(), this will take s, one string array result, an array positions, dotCount, this is initialize it with 3, startIndex, this is initialize it with 0,

  • if not dotCount is non-zero and ((size of s - 1) - startIndex + 1) 1, then −

    • temp := convertToNum(s, startIndex, size of s)

    • if temp >= 0 and temp <= 255, then−

      • insert temp at the end of positions

      • res := addDots(positions)

      • if size of res is same as size of s, then −

        • insert res at the end of result

    • return

  • for initialize i := startIndex, when i < size of s, update (increase i by 1), do −

    • temp := convertToNum(s, startIndex, i)

    • if temp >= 0 and temp <= 255, then −

      • insert temp at the end of positions

      • solve(s, result, positions, dotCount - 1, i + 1)

      • delete last element from positions

  • Define one function genIp this will take a string s,

  • Define an array result

  • Define an array position

  • solve(s, result, position)

  • return result

  • From the main method call genIp(A)

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
typedef long long int lli;
class Solution {
public:
   lli convertToNum(string s,int start, int end){
      lli num = 0;
      for (int i = start; i <= end; i++) {
         num = (num * 10) + (s[i] - '0');
         if (num > 255)
            return 10000;
      }
      return num;
}
string addDots(vector <int> positions){
   string res = "";
   int x = 0;
   int posIndex = 0;
   for (int i = 0; i < positions.size(); i++) {
      int num = positions[i];
      ostringstream str1;
      str1 << num;
      string temp = str1.str();
      res += temp;
      if (i < positions.size() - 1)
         res += ".";
   }
   return res;
}
void solve(string s, vector <string> &result,vector <int> positions, int dotCount = 3, int startIndex = 0){
   if (!dotCount && ((s.size() - 1) - startIndex + 1) >= 1) {
      int temp = convertToNum(s, startIndex, s.size() - 1);
      if (temp >= 0 && temp <= 255) {
         positions.push_back(temp);
         string res = addDots(positions);
         if (res.size() - 3 == s.size()) {
            result.push_back(res);
         }
      }
      return;
   }
   for (int i = startIndex; i < s.size(); i++) {
      int temp = convertToNum(s, startIndex, i);
      if (temp >= 0 && temp <= 255) {
         positions.push_back(temp);
         solve(s, result, positions, dotCount - 1, i + 1);
         positions.pop_back();
      }
   }
}
vector<string> genIp(string s){
   vector<string> result;
   vector<int> position;
   solve(s, result, position);
   return result;
}
vector<string> restoreIpAddresses(string A) {
   return genIp(A);
}};
main(){
   Solution ob;
   print_vector(ob.restoreIpAddresses("25525511135"));
}

Input

"25525511135"

Output

[255.255.11.135, 255.255.111.35, ]

Updated on: 17-Nov-2020

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements