classSolution { public: /** * @param s: A string * @return: A list of lists of string */ vector<vector<string> > partition(string s){ // write your code here vector<vector<string>> rs; vector<string> path;
dfs(rs, path, s, 0);
return rs; }
voiddfs(vector<vector<string> > &rs, vector<string> &path, conststring &s, int start){ if (start == s.size()) { rs.push_back(path); return; }
for (int i = start; i < s.size(); i++) { if (isPalindrome(s, start, i)) { path.push_back(s.substr(start, i - start + 1)); dfs(rs, path, s, i + 1); path.pop_back(); } } }
boolisPalindrome(conststring s, int start, intend){ while (start < end && s[start] == s[end]) { start++; end--; }