首页 > 代码库 > leetcode 131. Palindrome Partitioning

leetcode 131. Palindrome Partitioning

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"]  ]
 1 vector <vector<string> > results; 2     vector <string> line; 3     vector<vector<string>> partition(string s)  4     { 5         getPartition(s, 0); 6         return results; 7     } 8      9     void getPartition(string s, int index)10     {11         if (index >= s.size())12         {13             results.push_back(line);14             return;15         }16         17         for (int i = index; i < s.size(); i++)18         {19             string ts = s.substr(index, i + 1 - index);20             if (isPalindrome(ts))21             {22                 line.push_back(ts);23                 getPartition(s, i + 1);24                 line.pop_back();25             }26         }27     }28     29     bool inline isPalindrome(string s)30     {31         int i = 0, j = s.size() - 1;32         while (i < j)33         {34             if (s[i] == s[j])35                 i++, j--;36             else37                 return false;38         }39         40         return true;41     }

 

leetcode 131. Palindrome Partitioning