
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all palindrome permutations of a string in C++
In this problem, we are given a string and we have to print all the palindromic permutations that are possible from the characters of that string.
Let’s take an example to understand the problem −
Input − string = ‘aabb’
Output − abba baab
To solve this problem we have to take the characters of the string and one by one generate all palindrome strings using these characters.
Step 1 − Check if the string is a palindrome or not, print ‘Not Possible’ if not.
Step 2 − If it can make palindrome, then make it into half and lexicographically select each letter of string.
Step 3 − Traverse through the permutations created and reverse the half side for even length string and for odd frequency, the odd character should be in-mid to create a palindrome.
Step 4 − print all the palindrome created.
Program to implement the algorithm −
Example
#include <bits/stdc++.h> using namespace std; #define M 26 bool isPalindrome(string str, int* freq){ memset(freq, 0, M * sizeof(int)); int l = str.length(); for (int i = 0; i < l; i++) freq[str[i] - 'a']++; int odd = 0; for (int i = 0; i < M; i++) if (freq[i] % 2 == 1) odd++; if ((l % 2 == 1 && odd == 1 ) || (l %2 == 0 && odd == 0)) return true; else return false; } string reverse(string str){ string rev = str; reverse(rev.begin(), rev.end()); return rev; } void generatePalindromePermutation(string str){ int freq[M]; if (!isPalindrome(str, freq)) return; int l = str.length(); string half =""; char oddC; for (int i = 0; i < M; i++) { if(freq[i] % 2 == 1) oddC = i + 'a'; half += string(freq[i] / 2, i + 'a'); } string palindrome; do { palindrome = half; if (l % 2 == 1) palindrome += oddC; palindrome += reverse(half); cout<<palindrome<<endl; } while (next_permutation(half.begin(), half.end())); } int main() { string str="abab"; cout<<"All palindrome permutations of "<<str<<" are :\n"; generatePalindromePermutation(str); return 0; }
Output
All palindrome permutations of abab are : abba baab
- Related Articles
- Print all permutations of a given string
- Print all permutations of a string in Java
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates in C++
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Print all the palindromic permutations of given string in alphabetic order in C++
- All permutations of a string using iteration?
- Java Program to print distinct permutations of a string
- Print all permutations with repetition of characters in C++
- Creating all possible unique permutations of a string in JavaScript
- Print all permutations in sorted (lexicographic) order in C++
- Print first n distinct permutations of string using itertools in Python
- How to find all possible permutations of a given string in Python?
