> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Palindrome Permutation II Python Solution

> Tested Python solution for LeetCode 267 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 267, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/palindrome-permutation-ii/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 267   # by problem number
lcpy gen -s palindrome_permutation_ii   # by problem name
```

## 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.

### Examples

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

```
Input: s = "abc"
Output: []
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindrome_permutation_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindrome_permutation_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n * (n/2)!) for generating each half permutation
    # Space: O(n)
    def generate_palindromes(self, s: str) -> list[str]:
        counts = Counter(s)
        mid = ""
        for char, count in counts.items():
            if count % 2:
                if mid:
                    return []
                mid = char
                counts[char] -= 1

        results: list[str] = []

        def build(current: str) -> None:
            if all(value == 0 for value in counts.values()):
                results.append(current)
                return
            for char in counts:
                if counts[char] > 0:
                    counts[char] -= 2
                    build(char + current + char)
                    counts[char] += 2

        build(mid)
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(n \* (n/2)!) for generating each half permutation | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.