Palindrome Permutation II - Problem
Given a string s, return all the palindromic permutations (without duplicates) of it.
You may return the answer in any order. If s has no palindromic permutation, return an empty list.
A palindromic permutation is a rearrangement of the string's characters that reads the same forwards and backwards.
Input & Output
Example 1 — Basic Case
$
Input:
s = "aab"
›
Output:
["aba"]
💡 Note:
Character frequencies: a=2, b=1. Only one odd frequency (b), so palindrome possible. Arranging pairs 'a' around center 'b' gives 'aba'.
Example 2 — Multiple Palindromes
$
Input:
s = "aabb"
›
Output:
["abba", "baab"]
💡 Note:
All characters have even frequencies. Can arrange pairs in 2 ways: 'a' pairs outside 'b' pairs gives 'abba', or 'b' pairs outside 'a' pairs gives 'baab'.
Example 3 — Impossible Case
$
Input:
s = "abc"
›
Output:
[]
💡 Note:
All characters have frequency 1 (3 odd frequencies). Since more than 1 character has odd frequency, no palindromic permutation is possible.
Constraints
- 1 ≤ s.length ≤ 16
- s contains only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Count character frequencies
2
Validation
Check if palindromes are possible
3
Generation
Build all unique palindromic arrangements
Key Takeaway
🎯 Key Insight: A palindrome can have at most one character with odd frequency - use this to validate early and build efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code