forked from DeepanshuProgrammer/GeeksforGeeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
24 August "Problem of the Day" Answer
35 lines (30 loc) · 1.06 KB
/
24 August "Problem of the Day" Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
GeeksforGeeks
The Question is :- "Generate IP Addresses"
Answer :-
class Solution{
public:
vector<string> genIp(string &s) {
// Your code here
vector<string>ans;
for(int a=1;a<=3;a++)
for(int b=1;b<=3;b++)
for(int c=1;c<=3;c++)
for(int d=1;d<=3;d++)
if(a+b+c+d==s.size()){
int A=stoi(s.substr(0,a));
int B=stoi(s.substr(a,b));
int C=stoi(s.substr(a+b,c));
int D=stoi(s.substr(a+b+c,d));
string temp;
if(A<=255 && B<=255 && C<=255 &&D<=255){
temp=to_string(A)+"."+to_string(B)+"."+to_string(C)+"."+to_string(D);
if(temp.size()==s.size()+3)
ans.push_back(temp);
}
}
return ans;
}
};
Hope you understand the answer, and complete it.
Stay Connected for daily Problem of the Day answers.
Thank you All!