Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool palindromeChecker(string s){
- for(int i = 0, j = s.size() - 1; i <= j; i++, j--){
- if(s[i] != s[j]){
- return false;
- }
- }
- return true;
- }
- void palindromePartition(string s, int index, vector<string> &result){
- // base case
- if(index >= s.size()){
- bool allPalindrome = true;
- for(string str : result){
- allPalindrome = allPalindrome && palindromeChecker(str);
- }
- if(allPalindrome){
- for(int i = 0; i < result.size(); i++){
- cout << '[' << result[i] << ']';
- }
- cout << '\n';
- }
- return;
- }
- // separate partition
- char ch = s[index];
- string chToStr = "";
- chToStr += ch;
- result.push_back(chToStr);
- palindromePartition(s, index + 1, result);
- result.pop_back();
- // no separate partition, to maintain order in partition, push in last subset only
- if(result.size() > 0){
- result[result.size() - 1] += chToStr;
- palindromePartition(s, index + 1, result);
- result[result.size() - 1].pop_back();
- }
- }
- int main() {
- // your code goes here
- string s;
- cin >> s;
- vector<string> result;
- palindromePartition(s, 0, result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement