You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB" Output: 8 Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC" Output: 188
Example 3:
Input: tiles = "V" Output: 1
Constraints:
1 <= tiles.length <= 7tiles consists of uppercase English letters.Problem Overview: You are given a set of letter tiles where each tile has a character printed on it. The task is to count how many distinct non‑empty sequences you can form using those tiles. Each tile can be used at most once per sequence, and sequences with the same letters but different orders count as different results.
Approach 1: Backtracking with Set (O(n! * n) time, O(n! * n) space)
This approach explicitly generates every possible sequence using backtracking. Start with an empty path and recursively append unused characters. At each step, mark a tile as used, append it to the current sequence, and explore deeper. Because duplicate characters may exist (e.g., "AAB"), store generated sequences in a set to avoid counting duplicates. Each recursive call inserts the current string into the set and continues exploring longer permutations. The algorithm essentially enumerates permutations of all possible lengths from 1 to n. Worst‑case time complexity is O(n! * n) because building and storing strings costs O(n), and the number of permutations grows factorially. Space complexity is also O(n! * n) due to storing all unique sequences.
Approach 2: DFS with Frequency Map (O(n! * n) time, O(n) space)
A more efficient strategy avoids generating duplicate sequences by tracking character counts with a frequency map. Build a map using a hash table where each character stores how many tiles remain. Then perform DFS: iterate through each character whose count is greater than zero, place it into the sequence, decrement its count, and recurse. Every time you choose a character, you increment the total number of valid sequences. After recursion, restore the count (classic backtracking step). Because the algorithm works directly on character frequencies, it never generates duplicate permutations even when tiles repeat. This dramatically reduces unnecessary work compared to storing strings in a set. Time complexity remains O(n! * n) in the worst case due to permutation growth, but practical performance is significantly better. Space complexity is O(n) for recursion depth and the frequency structure.
Both methods rely on recursive enumeration of sequences derived from a string. The key difference is whether duplicates are filtered afterward with a set or prevented during generation using frequency counts.
Recommended for interviews: DFS with a frequency map is the approach most interviewers expect. It demonstrates understanding of permutation generation, pruning duplicate work, and efficient counting. Implementing the set‑based backtracking first can help you reason about the search space, but the frequency‑map DFS shows stronger algorithmic optimization and cleaner space usage.
This approach uses backtracking to explore all possible permutations of the tile letters. The permutations are stored in a set to ensure uniqueness, allowing us to retrieve non-duplicate sequences effectively. We recursively select tiles, marking them as used, and backtrack to explore other possibilities.
This solution uses backtracking to explore all possible permutations. We maintain an array called used to check if a tile has been included in the current permutation. If so, it's skipped in further permutations unless it's a unique arrangement. The results are collected into a set to ensure every sequence is unique.
Time Complexity: O(n * n!) where n is the number of tiles, considering permutations with duplicates.
Space Complexity: O(n * n!) for storing the permutations in the set.
This approach utilizes Depth-First Search (DFS) with a frequency map to track the count of each character used, allowing us to construct sequences by branching through available letters. It eliminates duplicate permutations by managing the count of each character carefully.
This solution builds all unique sequences using a frequency map (Counter) and explores different possibilities using a depth-first search mechanism. It examines each possible character by decreasing its availability in the counter, counting each new sequence formed uniquely as characters deplete.
Time Complexity: O(k^n), where k is the number of unique tiles and n is the total number of tiles.
Space Complexity: O(k), primarily due to the stored frequency map.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Backtracking with Set | Time Complexity: O(n * n!) where n is the number of tiles, considering permutations with duplicates. |
| DFS with Frequency Map | Time Complexity: O(k^n), where k is the number of unique tiles and n is the total number of tiles. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Set | O(n! * n) | O(n! * n) | Good for understanding permutation generation and handling duplicates with a set |
| DFS with Frequency Map | O(n! * n) | O(n) | Preferred approach when tiles contain duplicates and you want to avoid storing all sequences |
Letter Tile Possibilities - Leetcode 1079 - Python • NeetCodeIO • 13,788 views views
Watch 9 more video solutions →Practice Letter Tile Possibilities with our built-in code editor and test cases.
Practice on FleetCode