forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambiguous-coordinates.cpp
45 lines (43 loc) · 1.29 KB
/
ambiguous-coordinates.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
// Time: O(n^4)
// Space: O(n^2)
class Solution {
public:
vector<string> ambiguousCoordinates(string S) {
vector<string> result;
for (int i = 1; i < S.length() - 2; ++i) {
const auto& lefts = make(S, 1, i);
const auto& rights = make(S, i + 1, S.length() - 2 - i);
for (const auto& left : lefts) {
for (const auto& right : rights) {
string s;
s += "(";
s += left;
s += ", ";
s += right;
s += ")";
result.emplace_back(move(s));
}
}
}
return result;
}
private:
// Time: O(n^2)
// Space: O(n^2)
vector<string> make(const string& S, int i, int n) {
vector<string> result;
for (int d = 1; d <= n; ++d) {
const auto& left = S.substr(i, d);
const auto& right = S.substr(i + d, n - d);
if ((left.front() != '0' || left == "0") &&
right.back() != '0') {
string s;
s += left;
s += (!right.empty() ? "." : "");
s += right;
result.emplace_back(move(s));
}
}
return result;
}
};