-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathrestore-ip-addresses.cpp
48 lines (44 loc) · 1.46 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
// Time: O(n^m) = O(3^4)
// Space: O(n * m) = O(3 * 4)
class Solution {
public:
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s) {
vector<string> result;
string cur;
restoreIpAddressesHelper(s, 0, 0, &cur, &result);
return result;
}
void restoreIpAddressesHelper(const string& s,
int start, int dots,
string *cur,
vector<string> *result) {
// Pruning to improve performance.
if (((4 - dots) * 3 < s.length() - start) ||
((4 - dots) > s.length() - start)) {
return;
}
if (start == s.length() && dots == 4) {
result->emplace_back(cur->begin(), prev(cur->end()));
} else {
for (int i = start; i < start + 3; ++i) {
string tmp = s.substr(start, i - start + 1);
if (i < s.length() && isValid(tmp)) {
tmp.push_back('.');
cur->append(tmp);
restoreIpAddressesHelper(s, i + 1, dots + 1, cur, result);
cur->resize(cur->length() - (i - start + 2));
}
}
}
}
bool isValid(const string &s) {
if (s.empty() || (s[0] == '0' && s != "0")) {
return false;
}
return stoi(s) < 256;
}
};