Jumble Puzzle Bins
You set the weekly word jumble for a local paper. The best puzzles are words built from exactly the same letters, like least, slate and stale. Readers mail in a long list of suggestions, and you want to sort them into bins, one per set of letters, to see which bins are big enough for a puzzle.
Your task
Write groupJumbles(words). Put two words in the same bin exactly when one is a rearrangement of the other: the same letters, each used the same number of times. Return the list of bins.
Order matters, so the answer is unique. Bins appear in the order their first word appears in words. Inside a bin, words keep their order from words. If a word is suggested twice, both copies go in its bin. An empty list gives [].
groupJumbles(words: string[]) → string[][]
Examples
words = ["least", "tea", "slate", "eat", "bus", "stale", "ate"]
[["least", "slate", "stale"], ["tea", "eat", "ate"], ["bus"]]
least, slate and stale share a, e, l, s, t; tea, eat and ate share a, e, t; bus gets its own bin. Bins follow the order of their first words: least, tea, bus.words = ["ton", "not", "onto"]
[["ton", "not"], ["onto"]]
onto uses the same kinds of letter as ton, but has two o's, so it is not a rearrangement and gets its own bin.Constraints
- 0 ≤ words.length ≤ 10,000
- 0 ≤ words[i].length ≤ 20
- Words contain only lowercase letters a to z.
Start with brute force
Compare each word with the first word of every bin so far, using a letter-count check like the one in Same Letter Tiles, and join the first match or start a new bin. If most words differ, that is about n² comparisons: around 50 million for 10,000 words, each reading up to 20 letters.
Hints
0 of 4 shown- Hint 1
Sort the letters of
slateand ofleastalphabetically. What do you get? - Hint 2
Every rearrangement sorts to the same string, and words with different letters never do. So the sorted letters work as the bin's label.
- Hint 3
Now go from a label to its bin in one step, keeping bins in creation order. Which structure maps a key to a list?
- Hint 4
Walk the words once. Build each word's label (sorted letters, or 26 letter counts joined into a string) and look it up in a map of label → bin. If missing, create an empty bin and add it to the map and to the result list. Then append the word to its bin.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Instead of comparing words with each other, give each word a label shared by all its rearrangements and nothing else. Grouping is then one map lookup per word.
Sorted letters make a good label. Trace [least, tea, slate, eat, bus, stale, ate]:
least→ labelaelst, which is new, so it starts bin 1.tea→aet, which is new, so it starts bin 2.slate→aelst, so it joins bin 1, which is now [least, slate].eat→aet, so it joins bin 2, which is now [tea, eat].bus→bsu, which is new, so it starts bin 3.stalejoins bin 1 andatejoins bin 2, so the result is [[least, slate, stale], [tea, eat, ate], [bus]].
The label must capture how many of each letter, not just which. A set of letters puts aab with abb; a sum of letter codes puts ad with bc. Sorted letters, or a string of 26 counts, get both right.
function groupJumbles(words) {
const binOf = new Map(); // sorted letters -> that bin
const bins = [];
for (const w of words) {
const label = [...w].sort().join("");
if (!binOf.has(label)) {
binOf.set(label, []);
bins.push(binOf.get(label)); // same list object, so order is kept
}
binOf.get(label).push(w);
}
return bins;
}def group_jumbles(words):
bin_of = {} # sorted letters -> that bin
bins = []
for w in words:
label = "".join(sorted(w))
if label not in bin_of:
bin_of[label] = []
bins.append(bin_of[label]) # same list object, so order is kept
bin_of[label].append(w)
return binsRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.