Problem 528503 · medium · Phase 05 Advanced Algorithms & Graphs

Distinct Letter Shuffles

backtracking · permutations · duplicates · counting

A word game asks for every distinct rearrangement of the letters of word. Because some letters repeat, swapping two identical letters does not create a new word. Return a list of all distinct rearrangements (strings), in any order.

Examples

Input:  word = "aab"
Output: ["aab", "aba", "baa"]

Input:  word = "zz"
Output: ["zz"]

Input:  word = ""
Output: [""]

Constraints

  • 0 <= len(word) <= 8, lowercase letters only
  • The answer has at most 8! / (repeats) strings; test inputs keep it under a few thousand.

Goals

  • Generate permutations of a multiset without duplicates using a letter counter
  • Use remaining counts instead of a used-flag array
Starting Python…