首页 > 代码库 > 131. Palindrome Partitioning

131. Palindrome Partitioning

 1 class Solution { 2 public: 3     vector<vector<string>> partition(string s) { 4         vector<vector<string>> result; 5         vector<string> path; 6         DFS(s, result, path, 0); 7         return result; 8     } 9 private:10     void DFS(string s, vector<vector<string>>& result, vector<string>& path, int start)11     {12         if (start == s.size()) {13             result.push_back(path);14             return;15         } else {16             for (int i = start; i < s.size(); ++ i) {17                 if (isParlindrome(s, start, i)) {18                     path.push_back(s.substr(start, i - start + 1));19                     DFS(s, result, path, i + 1);20                     path.pop_back();21                 }22             }23         }24     }25 26     bool isParlindrome(string s, int left, int right)27     {28         if (s.size() == 0) return false;29         else {30             while (left < right) {31                 if (s[left] == s[right]) {32                     ++ left; -- right;33                 } else {34                     return false;35                 }36             }37             return true;38         }39     }40 };

 

131. Palindrome Partitioning