forked from Soumik-7031/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartitioningCpp
More file actions
32 lines (30 loc) · 859 Bytes
/
PalindromePartitioningCpp
File metadata and controls
32 lines (30 loc) · 859 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
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > res;
vector<string> path;
func(0, s, path, res);
return res;
}
void func(int index, string s, vector<string> &path,
vector<vector<string> > &res) {
if(index == s.size()) {
res.push_back(path);
return;
}
for(int i = index; i < s.size(); ++i) {
if(isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + 1));
func(i+1, s, path, res);
path.pop_back();
}
}
}
bool isPalindrome(string s, int start, int end) {
while(start <= end) {
if(s[start++] != s[end--])
return false;
}
return true;
}
};