-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.cpp
More file actions
53 lines (45 loc) · 1.34 KB
/
Solution.cpp
File metadata and controls
53 lines (45 loc) · 1.34 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class Solution {
public:
bool isPart(string str) {
if (str.size() > 3 or str.size() == 0) return false;
int i = stoi(str);
return i <= 255 and 0 <= i and (i == 0 ? str.size() == 1 : str[0] != '0');
}
vector<string> ipFromNPart(int n, string str) {
vector<string> res ;
if (n == 1) {
if (this->isPart(str)) res.push_back(str);
return res;
}
for (int i=1; i<=3 and i < str.size() ; i++) {
if (this->isPart(str.substr(0,i))) {
auto possible_postfix = this->ipFromNPart(n-1,str.substr(i));
for (auto word: possible_postfix) {
auto append = str.substr(0,i) + "." + word;
res.push_back(append);
}
}
}
return res;
}
// Input: "25525511135"
// Output: ["255.255.11.135", "255.255.111.35"]
vector<string> restoreIpAddresses(string s) {
return this->ipFromNPart(4, s);
}
};
int main() {
Solution s;
auto x = s.restoreIpAddresses("25525511135");
// auto x = s.restoreIpAddresses("0000");
// auto x = s.restoreIpAddresses("010010");
for (auto i: x) {
cout << i << endl;
}
}