class Solution {
public:
vector<vector<string>>res;
unordered_set<string>Hui;
bool isHui(string s) {
int it1 = 0, it2 = s.length() - 1;
while (it1 < it2) {
if (s[it1] != s[it2])return false;
++it1;
--it2;
}
return true;
}
void construct_s(vector<string>temp,string s, int index) {
int i = 1;
while (1) {
if (index == s.length())res.push_back(temp);
if (index + i > s.length())break;
if (i == 1) {
temp.push_back(s.substr(index, 1));
construct_s(temp, s, index + 1);
temp.pop_back();
}
else {
string sub_str = s.substr(index, i);
if (Hui.count(sub_str) || isHui(sub_str)) {
temp.push_back(sub_str);
construct_s(temp, s, index + i);
temp.pop_back();
}
}
++i;
}
}
vector<vector<string>> partition(string s) {
vector<string>temp;
construct_s(temp, s, 0);
return res;
}
};
131. Palindrome Partitioning
最新推荐文章于 2021-12-25 16:25:36 发布