-
Notifications
You must be signed in to change notification settings - Fork 28
/
Restore_IP_Addresses.cpp
50 lines (49 loc) · 1.59 KB
/
Restore_IP_Addresses.cpp
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
class Solution {
void restoreIpAddressesRecur(int indx, int cnt, string const& s, string& ipAddr, vector<string>& result) {
if(indx >= s.length()) {
return;
}
if(cnt == 3) {
if(indx < s.length() - 1 and s[indx] == '0') {
return;
}
string octet = s.substr(indx);
if(octet.length() > 3) {
return;
}
int iOctet = stoi(octet);
if(iOctet >= 0 and iOctet <= 255) {
ipAddr += octet;
result.push_back(ipAddr);
ipAddr.erase(ipAddr.length() - octet.length(), octet.length());
}
return;
}
int maxLen = min((int) s.length(), indx + 3);
if(s[indx] == '0') {
maxLen = indx + 1;
}
for(int len = indx + 1; len <= maxLen; ++len) {
string octet = s.substr(indx, len - indx);
int iOctet = stoi(octet);
if(iOctet >= 0 and iOctet <= 255) {
ipAddr += octet;
ipAddr += '.';
restoreIpAddressesRecur(len, cnt + 1, s, ipAddr, result);
ipAddr.erase(ipAddr.length() - octet.length() - 1, octet.length() + 1);
}
}
}
public:
vector<string> restoreIpAddresses(string s) {
vector<string> result;
int len = s.length();
if(len < 4 or len > 12) {
return result;
}
string ipAddr;
ipAddr.reserve(len + 4);
restoreIpAddressesRecur(0, 0, s, ipAddr, result);
return result;
}
};