fork download
  1. class Solution {
  2. public:
  3.  
  4. void partitionHelper(int index, string s, vector < string > & path,
  5. vector < vector < string > > & res) {
  6. if (index == s.size()) {
  7. res.push_back(path);
  8. return;
  9. }
  10. for (int i = index; i < s.size(); ++i) {
  11. if (isPalindrome(s, index, i)) {
  12. path.push_back(s.substr(index, i - index + 1));
  13. partitionHelper(i + 1, s, path, res);
  14. path.pop_back();
  15. }
  16. }
  17. }
  18.  
  19. bool isPalindrome(string s, int start, int end) {
  20. while (start <= end) {
  21. if (s[start++] != s[end--])
  22. return false;
  23. }
  24. return true;
  25. }
  26.  
  27. vector<vector<string>> partition(string s)
  28. {
  29. vector < vector < string > > res;
  30. vector < string > path;
  31. partitionHelper(0, s, path, res);
  32. return res;
  33. }
  34. };
Success #stdin #stdout 0.01s 5424KB
stdin
Standard input is empty
stdout
�����