Skip to the code
cf.
+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^4
  • 0 <= words[i].length <= 100
  • Words use lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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