Palindrome Partitioning
Total Accepted: 21056 Total Submissions: 81036Given 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"
,
[ ["aa","b"], ["a","a","b"] ]
vector>res;bool is_palindrome(const string &s, int start, int end){ while(start < end){ if(s[start] != s[end - 1]) return false; ++start; --end; } return true;}void dfs(const string &s, int cur, vector & partitions){ int size = s.size(); if(cur == size){ res.push_back(partitions); } for(int end = cur + 1; end <= size; ++end){ if(is_palindrome(s, cur, end)){ partitions.push_back(s.substr(cur, end - cur)); dfs(s, end, partitions); partitions.pop_back(); } }}vector > partition(string s) { vector tem; dfs(s, 0, tem); return res;}