Problem 212880 · medium · Phase 02 Linear Data Structures

Group Shuffled Words

hash maps · grouping · canonical keys

Two words are shuffles of each other if they use exactly the same letters with the same multiplicities. Group the words into shuffle classes and return the groups as a list of lists, where each group is sorted alphabetically and the groups are ordered by their first (smallest) word. Identical words stay in the same group and are each kept.

Examples

Input:  words = ["tea", "eat", "tan", "ate", "nat", "bat"]
Output: [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]

Input:  words = ["a", "a"]
Output: [["a", "a"]]

Constraints

  • 0 <= len(words) <= 2 * 10**4, 0 <= len(words[i]) <= 20, lowercase letters
  • Target complexity: O(total characters * log word length) time.

Goals

  • Derive a canonical key so equivalent items collide
  • Produce fully deterministic nested output
Starting Python…