-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestoreIPAddress.cpp
More file actions
50 lines (44 loc) · 1.25 KB
/
RestoreIPAddress.cpp
File metadata and controls
50 lines (44 loc) · 1.25 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
/**
Given a string containing only digits, restore it by returning all possible
valid IP address combinations.
For example:
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
*/
class Solution {
public:
bool isValid(string s) {
//note the rules of a valid ip address
if (s.size() ==3 && (atoi(s.c_str()) ==0 || atoi(s.c_str()) > 255)) return false;
if (s.size() ==3 && s[0] == '0' ) return false;
if (s.size() ==2 && s[0] == '0' ) return false;
if (s.size() ==2 && atoi(s.c_str()) ==0) return false;
return true;
}
void getRes(vector<string>& res, string ip, string s, int m) {
//each ip address has 4 parts and each part has length 1 up to 3
if (m ==0) {
//need to make sure that we don't have any digit remain
if (s.empty())
res.push_back(ip);
return ;
}
//iterate over the length of each part
for (int i = 1; i <=3; ++i) {
if (s.size() >= i && isValid(s.substr(0,i))) {
if (m ==1) {
getRes(res, ip + s.substr(0,i), s.substr(i), m-1);
}
else {
getRes(res, ip + s.substr(0,i) + ".", s.substr(i), m -1);
}
}
}
}
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
string ip= "";
getRes(ret, ip, s, 4);
return ret;
}
};