-
Notifications
You must be signed in to change notification settings - Fork 7
/
131.cpp
34 lines (29 loc) · 855 Bytes
/
131.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
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> path;
dfs(s,path,res);
return res;
}
void dfs(string s,vector<string> &path,vector<vector<string>> &res){
if(s.empty()) res.push_back(path);
for(int len = 1;len <= s.size();len++){
string substr = s.substr(0,len);
if(isPalindrome(substr)){
path.push_back(substr);
dfs(s.substr(len),path,res);
path.pop_back();
}
}
}
private:
bool isPalindrome(const string &str){
int l = 0,r = str.size() - 1;
while(l < r){
if(str[l++] == str[r--]) continue;
else return false;
}
return true;
}
};