- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all palindromic partitions of a string in C++
In this problem, we are given a palindromic string. And we have to print all the partitions of this string. In this problem, we will find all possible palindrome partitions of the string by cutting it.
Let’s take an example to understand the problem -
Input − string = ‘ababa’
Output − ababa , a bab a, a b a b a ….
The solution, to this problem, is to check if a substring is a palindrome or not. And print the substring if it is substring.
Example
The below program will illustrate the solution −
#include<bits/stdc++.h> using namespace std; bool isPalindrome(string str, int low, int high){ while (low < high) { if (str[low] != str[high]) return false; low++; high--; } return true; } void palindromePartition(vector<vector<string> >&allPart, vector<string> &currPart, int start, int n, string str){ if (start >= n) { allPart.push_back(currPart); return; } for (int i=start; i<n; i++){ if (isPalindrome(str, start, i)) { currPart.push_back(str.substr(start, i-start+1)); palindromePartition(allPart, currPart, i+1, n, str); currPart.pop_back(); } } } void generatePalindromePartitions(string str){ int n = str.length(); vector<vector<string> > partitions; vector<string> currPart; palindromePartition(partitions, currPart, 0, n, str); for (int i=0; i< partitions.size(); i++ ) { for (int j=0; j<partitions[i].size(); j++) cout<<partitions[i][j]<<" "; cout<<endl; } } int main() { string str = "abaaba"; cout<<"Palindromic partitions are :\n"; generatePalindromePartitions(str); return 0; }
Output
Palindromic partitions are : a b a a b a a b a aba a b aa b a a baab a aba a b a aba aba abaaba
- Related Articles
- Print all the palindromic permutations of given string in alphabetic order in C++
- Count all Palindromic Subsequence in a given String in C++
- Print all subsequences of a string in C++
- Print all palindrome permutations of a string in C++
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Print all subsequences of a string using ArrayList in C++
- Counting all possible palindromic subsequence within a string in JavaScript
- Find all distinct palindromic sub-strings of a given String in Python
- Print all funny words in a string in C++
- Print all distinct characters of a string in order in C++
- Program to print all substrings of a given string in C++
- Print all subsequences of a string using Iterative Method in C++
- Find all palindromic sub-strings of a given string - Set 2 in Python
- C Program to print all permutations of a given string
- Print all permutations of a string in Java

Advertisements