Application of Backtracking
Algorithms and Data Structures: TheAlgorist.com
System Design: DistributedComputing.dev
Low Level Design: LowLevelDesign.io
Frontend Engineering: FrontendEngineering.io
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
Output: ["abba", "baab"]
- 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.
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.
This is a Premium content.
Please subscribe to Algorithms course to access the code.
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:
Senior SDE | Chief Architect
Microsoft | University of Florida
If you have any feedback, please use this form: https://thealgorists.com/Feedback.