-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131. Palindrome Partitioning.cpp
49 lines (42 loc) · 1.2 KB
/
131. Palindrome Partitioning.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
// ---------------------------
// Error
// ---------------------------
// The drive or UNC share you selected does not exist or is not accessible. Please select another.
// ---------------------------
// OK
// ---------------------------
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> result;
vector<string> curr;
int lenS = s.length();
function<void(int)> explore = [&](int index) {
if (index >= lenS) {
result.push_back(curr);
return;
}
for (int i = index; i < lenS; ++i) {
string subStr = s.substr(index, i - index + 1);
if (isPalindrome(subStr)) {
curr.push_back(subStr);
explore(i + 1);
curr.pop_back();
}
}
};
explore(0);
return result;
}
private:
bool isPalindrome(const string& s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right])
return false;
++left;
--right;
}
return true;
}
};