Letter Tile Possibilities

Count distinct non-empty sequences buildable from tiles with possibly-duplicate letters — DFS over a frequency map.

Medium
Time O(n!) Space O(|Σ|)
LeetCode
3 min read
subsets backtracking

Problem#

Given a string of uppercase letters (multiset of tiles), return the number of distinct non-empty sequences you can form by ordering some subset of them.

Examples#

  • "AAB"8 ("A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA")

Constraints#

  • 1 <= s.length <= 7.

Hints#

Hint 1
Frequency map; DFS over the 26 letters at each step. Each pick contributes 1 + sub-count.

Approach#

Frequency vector of length 26. DFS: at each call, for each letter with count > 0, “place it” (decrement), count this as 1 sequence + the count from the recursive subtree.

Solution#

Letter Tile Possibilities
class Solution {
public:
int numTilePossibilities(string tiles) {
int cnt[26] = {0};
for (char c : tiles) ++cnt[c - 'A'];
function<int()> dfs = [&]() {
int sum = 0;
for (int i = 0; i < 26; ++i) {
if (cnt[i] == 0) continue;
--cnt[i];
sum += 1 + dfs();
++cnt[i];
}
return sum;
};
return dfs();
}
};

Editorial#

Counting on the multiset side avoids enumerating sequences as strings. The DFS implicitly dedups: identical tiles are picked from the same bucket, never producing duplicate sequences. Each recursive call adds one for the new pick and recurses for extensions.

Complexity#

  • Time: O(n!) in the worst (all distinct) case.
  • Space: O(|Σ|).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.