Skip to the code
+25 XP

#33 Bucket Words by Their Letters

medium

A word puzzle app wants to show which words in words are rearrangements of each other: two words belong together when they use exactly the same letters the same number of times.

Write bucketWords(words) that returns the groups as arrays. List the groups in the order their first word appears in words, and keep the words inside each group in their original order.

Example 1

Input: words = ["listen","enlist","google","silent","elgoog"]
Output: [["listen","enlist","silent"],["google","elgoog"]]

listen, enlist, and silent share their letters, and so do google and elgoog.

Example 2

Input: words = ["abc"]
Output: [["abc"]]

A single word is a group of one.

Constraints

  • 0 <= words.length <= 10^5
  • 0 <= words[i].length <= 100
  • All the words together have at most 10^6 letters.
  • Words use lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).