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

# Letter Tile Possibilities Python Solution

> Tested Python solution for LeetCode 1079 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1079, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/letter-tile-possibilities/description/).

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

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

## Problem

You have \<code>n\</code>  \<code>tiles\</code>, where each tile has one letter \<code>tiles\[i]\</code> printed on it.

Return \<em>the number of possible non-empty sequences of letters\</em> you can make using the letters printed on those \<code>tiles\</code>.

### Examples

```
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
```

```
Input: tiles = "AAABBC"
Output: 188
```

```
Input: tiles = "V"
Output: 1
```

### Constraints

* \<code>1 \<= tiles.length \<= 7\</code>
* \<code>tiles\</code> consists of uppercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n!) bounded by distinct-letter tree size
    # Space: O(n)
    def num_tile_possibilities(self, tiles: str) -> int:
        counts: dict[str, int] = {}
        for ch in tiles:
            counts[ch] = counts.get(ch, 0) + 1

        def dfs() -> int:
            total = 0
            for ch in counts:
                if counts[ch] == 0:
                    continue
                counts[ch] -= 1
                total += 1 + dfs()
                counts[ch] += 1
            return total

        return dfs()
```

## Complexity

| Time | Space |
| - | - |
| O(n!) bounded by distinct-letter tree size | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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