Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
Solution: ...
1 class Solution { 2 public: 3 vector<vector<string>> res; 4 vector<vector<string>> partition(string s) { 5 res.clear(); 6 vector<string> part; 7 partitionRe(s, 0, part); 8 return res; 9 } 10 11 void partitionRe(const string &s, int start, vector<string> &part) { 12 if (start == s.size()) 13 { 14 res.push_back(part); 15 return; 16 } 17 string palindrom; 18 for (int i = start; i < s.size(); ++i) { 19 palindrom.push_back(s[i]); 20 if (!isPalindrome(palindrom)) continue; 21 part.push_back(palindrom); 22 partitionRe(s, i + 1, part); 23 part.pop_back(); 24 } 25 } 26 27 bool isPalindrome(const string &s) { 28 int i = 0, j = s.size()-1; 29 while (i < j) { 30 if (s[i] != s[j]) return false; 31 i++; j--; 32 } 33 return true; 34 } 35 };