Palindrome Permutation II - Problem
Palindrome Permutation II is a fascinating string manipulation problem that combines permutation generation with palindrome validation.

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
The Mirror Strategy for PalindromesStep 1: Count CharactersInput: "aabbcc"a: 2b: 2c: 2✓ All even - possible!Step 2: Build Half + MirrorHalf characters: [a, b, c] (one of each pair)Arrangement 1:abcmirrorcba→ "abccba"Step 3: All Possible ArrangementsHalf: [a,b,c] → abccbaHalf: [a,c,b] → acbbcaHalf: [b,a,c] → baccabHalf: [b,c,a] → bcaacbHalf: [c,a,b] → cabbacHalf: [c,b,a] → cbabc6 unique palindromic permutations!🎯 Key Insight: By permuting only half the characters and mirroring, we avoid generating non-palindromic strings!
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

n
2n
Quadratic Growth
Space Complexity
O(n! × n)

Storing all permutations, each of length n

n
2n
Quadratic Space

Constraints

  • 1 ≤ s.length ≤ 16
  • s consists of only lowercase English letters
  • Note: The relatively small constraint on length makes backtracking feasible
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen