Skip to content

Commit

Permalink
Time: 10 ms (13.15%), Space: 7 MB (24.93%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Jan 21, 2023
1 parent bacfe2a commit 340f98c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 0093-restore-ip-addresses/0093-restore-ip-addresses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {
public:
vector<string> ans;
string str="";
void solve(string s, int i,int k)
{
//base case
if(k==4 && i>=s.size())
{
str.pop_back();
ans.push_back(str);
str +='.';
}
if(k>4)return;


//take 1
if(i<s.size()){

str+=s[i];
str+=".";
solve(s,i+1,k+1);

str.pop_back();
str.pop_back();

}
//take 2
if(i+1<s.size() && s[i]!='0')
{
str+=s[i];
str+=s[i+1];
str+=".";
solve(s,i+2,k+1);
str.pop_back();
str.pop_back();
str.pop_back();
}
//take 3
if(i+2<s.size() && s[i]!='0' && stoi(s.substr(i, 3)) <= 255)
{
str+=s[i];
str+=s[i+1];
str+=s[i+2];
str+=".";
solve(s,i+3,k+1);
str.pop_back();
str.pop_back();
str.pop_back();
str.pop_back();
}
return;
}
vector<string> restoreIpAddresses(string s) {
solve(s,0,0);
return ans;
}
};

0 comments on commit 340f98c

Please sign in to comment.