Problem 514274 · medium · Phase 05 Advanced Algorithms & Graphs

How Many Words From Letter Tiles

backtracking · counting · duplicates · permutations

A word game gives you a rack of letter tiles tiles (letters may repeat). A play is any non-empty sequence of tiles from the rack, each tile used at most once. Two plays are the same if they spell the same string. Return how many different strings can be played.

Examples

Input:  tiles = "abc"
Output: 15
Explanation: 3 of length 1, 6 of length 2, 6 of length 3.

Input:  tiles = "zz"
Output: 2
Explanation: "z" and "zz".

Input:  tiles = "q"
Output: 1

Constraints

  • 0 <= len(tiles) <= 7, lowercase letters

Goals

  • Count distinct arrangements of every length from a multiset
  • Avoid double counting by branching on distinct letters, not on tiles
Starting Python…