131. Palindrome Partitioning
problem description
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]algorithm thought
code
class Solution {
public:
//unordered_map<int,bool> ma;
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> tmp;
helper(s,res,tmp,0);
return res;
}
void helper(string&s,vector<vector<string>>&res,vector<string>&tmp,int begin){
if(begin==s.size()){
res.push_back(tmp);
}
for(int i=begin;i<s.size();++i){
if(isPalindrome(s,begin,i)){
tmp.push_back(std::move(s.substr(begin,i-begin+1)));
helper(s,res,tmp,i+1);
tmp.pop_back();
}
}
}
bool isPalindrome(string&s,int begin,int end){
while(begin<end){
if(s[begin++]!=s[end--])
return false;
}
return true;
}
};algorithm analysis
Last updated