-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition.cpp
More file actions
30 lines (30 loc) · 833 Bytes
/
partition.cpp
File metadata and controls
30 lines (30 loc) · 833 Bytes
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
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
vector<string> t;
t.reserve(n);
vector<vector<string>> ans;
function<bool(int, int)> check = [&](int l, int r) -> bool {
while (l < r) {
if (s[l++] != s[r--]) return false;
}
return true;
};
function<void(int)> f = [&](int start) {
if (start == n) {
ans.push_back(t);
return;
}
for (int i = start; i < n; ++i) {
if (check(start, i)) {
t.push_back(s.substr(start, i - start + 1));
f(i + 1);
t.pop_back();
}
}
};
f(0);
return ans;
}
};