-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPalindrome_Partitioning.cpp
More file actions
34 lines (30 loc) · 936 Bytes
/
Palindrome_Partitioning.cpp
File metadata and controls
34 lines (30 loc) · 936 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
31
32
33
34
class Solution {
public:
bool isPalindrome(string &str, int start, int end) {
while (start < end) {
if (str[end--] != str[start++]) {
return false;
}
}
return true;
}
void partitionUtils(string s, int start, vector<string> &r, vector<vector<string> > &res) {
if (start >= s.size()) {
res.push_back(r);
return;
}
for (int i = start; i < s.size(); i++) {
if(isPalindrome(s, start, i) ) {
r.push_back(s.substr(start, i - start + 1));
partitionUtils(s, i + 1, r, res);
r.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string> > res;
vector<string> r;
partitionUtils(s, 0, r, res);
return res;
}
};