Problem Statement:


Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.

Example 1:
Input: "aabb"
Output: ["abba", "baab"]

Example 2:
Input: "abc"
Output: []

Solution:


  • NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rock-solid understanding.


Prerequisites:

We got the solution for this problem just by extending the solution for Permutations. We compute all permutations and for each permutation we check if it is a palindrome or not. I am not adding any more details here, since the core algorithm is already discussed in Permutations chapter, which is a prerequisite for this chapter.



Please subscribe to access the code.
After subscribing please come back and refresh this page.




Further Optimization: PRUNING


We don't not need to unnecessarily spend time computing a permutation if we already know that this permutation is not going to be a palindrome. So we PRUNE the search to avoid unnecessary computation and improve on time.

Please see the inline comment which marks the optimizations done.



Login to Access Content




Don't forget to take an in-depth look at the other backtracking problems in the below link, because that is what would make you comfortable with using the backtracking template and master the art of Backtracking and be an over-achiever:



Instructor:



If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Help Your Friends save 40% on our products

wave