Group Words by Letters

MediumHash Map≈ 25 min

You are given a list of lowercase words. Two words belong together when one can be spelled by rearranging the letters of the other: "stop" and "pots" do, "stop" and "cat" do not.

Return the words in groups, one group per set of letters. The groups can come in any order, and so can the words inside each group. A word that appears twice appears twice in its group.

Examples

text
["stop", "pots", "tops", "cat", "act", "dog"]
  →  [["stop", "pots", "tops"], ["cat", "act"], ["dog"]]

["x"]  →  [["x"]]

Constraints

  • Between 1 and 5,000 words, each at most 20 letters from a to z.
  • Aim for a solution that handles each word once: O(n) in the number of words (each word's length is small and bounded).
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 6 on the judge, 3 of them hidden, and measures how your solution scales.