-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestore-ip-addresses.cpp
105 lines (97 loc) · 2.41 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// https://leetcode.com/problems/restore-ip-addresses/
// Backtracking
class Solution
{
bool valid(const string &s, int start, int length)
{
return length == 1 || (s[start] != '0' && (length < 3 || s.substr(start, length) <= "255"));
}
void helper(const string &s, int startIndex, vector<int> &dots, vector<string> &ans)
{
const int remainingLength = s.length() - startIndex;
const int remainingNumberOfIntegers = 4 - dots.size();
if (remainingLength > remainingNumberOfIntegers * 3 ||
remainingLength < remainingNumberOfIntegers)
{
return;
}
if (dots.size() == 3)
{
if (valid(s, startIndex, remainingLength))
{
string temp;
int last = 0;
for (int dot : dots)
{
temp.append(s.substr(last, dot));
last += dot;
temp.append(".");
}
temp.append(s.substr(startIndex));
ans.push_back(temp);
}
return;
}
for (int curPos = 1; curPos <= 3 && curPos <= remainingLength; ++curPos)
{
// Append a dot at the current position.
dots.push_back(curPos);
// Try making all combinations with the remaining string.
if (valid(s, startIndex, curPos))
{
helper(s, startIndex + curPos, dots, ans);
}
// Backtrack, i.e. remove the dot to try placing it at the next position.
dots.pop_back();
}
}
public:
vector<string> restoreIpAddresses(string s)
{
vector<int> dots;
vector<string> ans;
helper(s, 0, dots, ans);
return ans;
}
};
// Iterative
class Solution
{
bool valid(const string &s, int start, int length)
{
return length == 1 || (s[start] != '0' && (length < 3 || s.substr(start, length) <= "255"));
}
public:
vector<string> restoreIpAddresses(string s)
{
vector<string> ans;
int length = s.length();
for (int len1 = max(1, length - 9); len1 <= 3 && len1 <= length - 3; ++len1)
{
if (!valid(s, 0, len1))
{
continue;
}
for (int len2 = max(1, length - len1 - 6); len2 <= 3 && len2 <= length - len1 - 2;
++len2)
{
if (!valid(s, len1, len2))
{
continue;
}
for (int len3 = max(1, length - len1 - len2 - 3);
len3 <= 3 && len3 <= length - len1 - len2 - 1; ++len3)
{
if (valid(s, len1 + len2, len3) &&
valid(s, len1 + len2 + len3, length - len1 - len2 - len3))
{
ans.push_back(s.substr(0, len1) + "." + s.substr(len1, len2) + "." +
s.substr(len1 + len2, len3) + "." +
s.substr(len1 + len2 + len3));
}
}
}
}
return ans;
}
};