Palindrome Permutation II - Problem
Palindrome Permutation II is a fascinating string manipulation problem that combines permutation generation with palindrome validation.
Given a string
Key Insight: Not all strings can form palindromic permutations! For a string to form a palindrome, at most one character can have an odd frequency (this would be the middle character).
Examples:
•
•
•
Given a string
s, your task is to return all possible palindromic permutations (without duplicates) of the input string. A palindrome reads the same forwards and backwards, like "aba" or "racecar".Key Insight: Not all strings can form palindromic permutations! For a string to form a palindrome, at most one character can have an odd frequency (this would be the middle character).
Examples:
•
"aab" → ["aba", "baa"]•
"abc" → [] (impossible to form palindrome)•
"aabbcc" → Multiple palindromic arrangements possible Input & Output
example_1.py — Basic case with possible palindromes
$
Input:
s = "aab"
›
Output:
["aba", "baa"]
💡 Note:
The string "aab" has frequencies: a=2, b=1. Since only one character (b) has odd frequency, palindromes are possible. We can form "aba" and "baa" by arranging the characters appropriately.
example_2.py — Impossible case
$
Input:
s = "abc"
›
Output:
[]
💡 Note:
The string "abc" has frequencies: a=1, b=1, c=1. Since three characters have odd frequencies (more than 1), it's impossible to form any palindromic permutations.
example_3.py — Multiple palindromes possible
$
Input:
s = "aabb"
›
Output:
["abba", "baab"]
💡 Note:
The string "aabb" has frequencies: a=2, b=2. Since all characters have even frequencies, multiple palindromic arrangements are possible by placing pairs symmetrically.
Visualization
Tap to expand
Understanding the Visualization
1
Count the Books
Count how many of each book type you have. For a mirror arrangement, you need pairs (except maybe one unique book for the center).
2
Check Feasibility
If you have more than one book type with odd count, a perfect mirror arrangement is impossible.
3
Arrange Half + Mirror
Place pairs of books symmetrically from outside-in. Any single book goes in the center, then the 'mirror' completes the palindrome.
4
Generate All Arrangements
Try different arrangements of the first half - each creates a unique palindromic permutation when mirrored.
Key Takeaway
🎯 Key Insight: Instead of generating all permutations and filtering, we build palindromes directly by permuting only half the characters and using the mirror property - this is exponentially more efficient!
Time & Space Complexity
Time Complexity
O(n! × n)
O(n!) to generate all permutations, O(n) to check each for palindrome property
⚠ Quadratic Growth
Space Complexity
O(n! × n)
Storing all permutations, each of length n
⚠ Quadratic Space
Constraints
- 1 ≤ s.length ≤ 16
- s consists of only lowercase English letters
- Note: The relatively small constraint on length makes backtracking feasible
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code