Skip to main content

Palindrome Permutation II - Solution & Explanation

MediumPremiumFree on FleetCodeHash TableStringBacktracking4 min read
Practice this problem

Problem Statement

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.

 

Example 1:

Input: s = "aabb"
Output: ["abba","baab"]

Example 2:

Input: s = "abc"
Output: []

 

Constraints:

  • 1 <= s.length <= 16
  • s consists of only lowercase English letters.

Approach Overview

Problem Overview: Given a string s, generate all unique permutations that form a palindrome. If no palindromic arrangement exists, return an empty list. The challenge is detecting whether a palindrome is possible and avoiding duplicate permutations.

Approach 1: Generate All Permutations + Palindrome Check (Brute Force) (Time: O(n! * n), Space: O(n))

This method generates every permutation of the string and checks whether each permutation is a palindrome. Use standard permutation generation with recursion or backtracking, then verify the result using a two-pointer palindrome check. The drawback is factorial growth: n! permutations are generated even though only a tiny subset could be valid palindromes. Duplicate permutations also appear when characters repeat, which requires additional filtering using a set. This approach works only for very small strings and mainly demonstrates baseline reasoning.

Approach 2: Hash Map + Half Permutation Backtracking (Time: O((n/2)!), Space: O(n))

A palindrome is symmetric. At most one character can appear an odd number of times. Start by counting character frequencies using a hash table. If more than one character has an odd frequency, forming a palindrome is impossible. Otherwise, build a half string containing freq[c] / 2 copies of each character. The center character (if any) is the one with the odd count.

Next, generate unique permutations of this half string using backtracking. Sorting or using a visited array prevents duplicate permutations when characters repeat. For every generated half permutation, construct the palindrome as half + middle + reverse(half). Because only half of the characters are permuted, the search space drops from n! to (n/2)!, which is dramatically smaller.

This method leverages symmetry and frequency counting to prune invalid cases early. The main operations are counting characters, recursively generating permutations of the half string, and mirroring it using simple string manipulation. Memory usage remains linear because only the recursion stack and frequency structures are stored.

Recommended for interviews: Interviewers expect the hash map + half-permutation backtracking solution. Recognizing the palindrome property (only one odd frequency allowed) shows strong problem decomposition. Mentioning the brute force permutation approach first shows understanding of the search space, but the optimized half-generation technique demonstrates algorithmic insight and practical efficiency.

Solution

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Generate All Permutations + Palindrome CheckO(n! * n)O(n)Conceptual baseline or very small strings
Hash Map + Half Permutation BacktrackingO((n/2)!)O(n)General case; optimal solution for interviews and large inputs

Video Solution

LeetCode 267. Palindrome Permutation II Explanation and Solution • happygirlzt • 3,818 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Palindrome Permutation II easy or hard?
Palindrome Permutation II is generally rated Medium difficulty. The challenge lies in recognizing the palindrome frequency property and reducing the permutation search space by generating only half of the string.
Palindrome Permutation II Python/Java solution
Typical implementations use a hash map to count characters, build a half-string list, and apply backtracking with a visited array to avoid duplicates. After generating a half permutation, the algorithm concatenates it with an optional middle character and the reversed half to produce a full palindrome.
How to solve Palindrome Permutation II in O(n)?
O(n) time is achievable only for the feasibility check, not for generating all palindromes. Counting character frequencies with a hash map and verifying that at most one character has an odd count takes O(n). Generating all valid permutations afterward requires factorial time relative to half the string length.
What is the best approach for Palindrome Permutation II?
The most efficient method counts character frequencies using a hash map, verifies that at most one character has an odd count, then generates permutations of only half the string using backtracking. Each half permutation is mirrored to form a full palindrome. This reduces the search space from n! permutations to roughly (n/2)! permutations.
Is Palindrome Permutation II asked at Google/Amazon/Meta?
Palindrome permutation and backtracking problems frequently appear in interviews at large tech companies including Google, Amazon, and Meta. Variations test understanding of character frequency counting, pruning invalid states, and generating unique permutations efficiently.
What data structure is used in Palindrome Permutation II?
A hash table (or frequency array) is used to count character occurrences. Backtracking with recursion generates unique permutations of half the string, and basic string operations mirror the result to form palindromes.
What is the time complexity of Palindrome Permutation II?
The optimized solution runs in O((n/2)!) time because only half of the characters are permuted. Building each palindrome takes O(n) time, but the dominant factor is the number of half permutations generated by backtracking. Space complexity is O(n) for recursion and result construction.

Ready to solve this problem?

Practice Palindrome Permutation II with our built-in code editor and test cases.

Practice on FleetCode