Open In App

Print all palindrome permutations of a string

Last Updated : 13 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a string s, consisting of lowercase Latin characters [a-z]. Find out all the possible palindromes that can be generated using the letters of the string and print them in lexicographical order.

Note: Return an empty array if no possible palindromic string can be formed.

Examples:

Input: s = aabcb
Output: { abcba bacab }
Explanation: “abcba” and “bacab” are possible strings that are palindrome.

Input: abc
Output: { }
Explanation: No possible palindromic string can be formed.

Approach:

The idea is to generate all permutations of the first half of the palindromic string and then concatenate its reverse to complete the whole string.

  • Create an array freq[] of size 26, to store the frequency of all the characters in string s.
  • Now check if the palindromic string can be formed using all the characters. Palindromic string can be formed if all the characters have even count or only one character has odd count.
  • If the condition is not satisfied, return the empty array, else construct the first half by considering the half frequency of each letter of string s.
  • Now traverse through all possible permutation of this half string and each time add reverse of this part at the end and add odd frequency character in mid between if string is of odd length, for making the palindrome.

Below is given the implementation

C++
// C++ program to print all palindromic 
// permutations of a given string
#include <bits/stdc++.h>
using namespace std;

// Function to find all possible
// palindromic strings of s
vector<string> all_palindromes(string &s) {
    
    // frequency array to count frequencies
    // of all characters of string s
    vector<int> freq(26, 0);
    for(auto i:s) {
        freq[i - 'a']++;
    }

    // odd will keep the count of letters
    // with odd occurrence
    int odd = 0;
    for(auto i:freq) {
        if(i % 2 != 0) {
            odd++;
        }
    }

    // if more than one letter have
    // odd frequency, return empty array
    if(odd > 1) 
        return {};

    int n = s.length();

    // to store first half of palindrome
    string half = "";

    // to store character with odd occurrence
    char oddC;

    for (int i = 0; i < 26; i++) {

        // store the character with odd frequency
        if(freq[i] % 2 == 1)
            oddC = i + 'a';

        // add half of the characters
        for(int j = 0; j < freq[i] / 2; j++) {
            half += (i + 'a');
        }
    }

    // to store all possible palindromic string
    vector<string> res;

    // generate all permutation of first half, and add
    // reverse of it at end. Also, add the character with
    // odd frequency in the mid
    do {

        string cur = half;

        // to store the reversed half
        string rev = cur;
        reverse(rev.begin(), rev.end());
        
        // add the character with odd frequency
        if (n % 2 == 1)
            cur += oddC;

        // add the reversed string
        cur += rev;
        res.push_back(cur);
    }
    // generate next permutation of first half
    while (next_permutation(half.begin(), half.end()));

    return res;
}

int main() {
    string s = "aabcb";
    vector<string> ans = all_palindromes(s);
    cout<< "{ ";
    for(auto i:ans) {
        cout<<i<<" ";
    }
    cout<<"}";
    return 0;
}
Java Python C# JavaScript

Output
{ abcba bacab }

Time Complexity: O((n / 2)!), where n is the length of the string s.
Auxiliary Space: O(1)



Next Article

Similar Reads

three90RightbarBannerImg