Letter Tile Possibilities - Problem
Letter Tile Possibilities

Imagine you have a collection of letter tiles, like those used in Scrabble or word games. Each tile has a single letter printed on it, and you want to know how many different sequences you can create using these tiles.

Given a string tiles where each character represents a letter on a tile, return the total number of possible non-empty sequences you can make. You can use each tile at most once in each sequence.

For example, with tiles "AAB", you can make: "A", "B", "AA", "AB", "BA", "AAB", and "ABA" - that's 7 different sequences!

Key Points:
• Each sequence must be non-empty
• You can use tiles in any order
• Each tile can only be used once per sequence
• Different arrangements of the same letters count as different sequences

Input & Output

example_1.py — Basic Case
$ Input: tiles = "AAB"
Output: 7
💡 Note: The possible sequences are: "A", "B", "AA", "AB", "BA", "AAB", "ABA". Note that "A" appears twice in our tiles, so sequences like "AA" are possible.
example_2.py — All Same Letters
$ Input: tiles = "AAABBC"
Output: 188
💡 Note: With 3 A's, 2 B's, and 1 C, we can form many different sequences. The algorithm counts all unique arrangements of different lengths efficiently.
example_3.py — Single Tile
$ Input: tiles = "V"
Output: 1
💡 Note: With only one tile, we can only form one sequence: "V". This is the simplest edge case.

Constraints

  • 1 ≤ tiles.length ≤ 7
  • tiles consists of uppercase English letters
  • Note: The small constraint on length makes backtracking very efficient

Visualization

Tap to expand
Letter Tile Sequence BuilderAABCharacter Counts: A:2, B:1Building Sequences:A→ Try extending with A, BB→ Try extending with A, AAll Possible SequencesLength 1:"A", "B"(2 total)Length 2:"AA", "AB", "BA"(3 total)Length 3:"AAB", "ABA"(2 total)Total: 7 sequences🎯 Key Insight: Backtracking with frequency counting avoids generating duplicate sequences
Understanding the Visualization
1
Count Your Tiles
First, count how many of each letter you have (A:2, B:1)
2
Try Each Letter
For each available letter type, use one and see what sequences you can extend
3
Build Recursively
At each step, count the current sequence plus all possible extensions
4
Backtrack Efficiently
Restore letter counts and try the next possibility
Key Takeaway
🎯 Key Insight: By using character frequency counting with backtracking, we can efficiently explore all possible sequences without generating duplicates, making the solution much faster than brute force permutation generation.
Asked in
Google 25 Amazon 18 Microsoft 12 Facebook 8
18.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen