Problem 465390 · medium · Phase 04 Non-Linear Data Structures

Distinct Letter Tile Arrangements

recursion · memoisation · permutations · multisets

A board game gives a player a rack of letter tiles described by the string tiles. Tiles with the same letter are indistinguishable. Write arrangements(tiles) returning the number of distinct words (orderings) that use all the tiles.

Examples

Input:  tiles = "aab"
Output: 3
Explanation: aab, aba, baa.

Input:  tiles = "abc"
Output: 6

Input:  tiles = "aaaa"
Output: 1

Constraints

  • 0 <= len(tiles) <= 12, lowercase letters; the empty rack has one (empty) arrangement.
  • Recursion depth is at most len(tiles) + 1.

Goals

  • Generate arrangements by choosing which letter type goes next, not which tile
  • Represent remaining tiles as a tuple of counts so the state is hashable
  • Memoise the count tuple to avoid recomputing identical remainders
Starting Python…