mediumArrays & HashingAmazonMicrosoftMeta
Lunch Menu Signature Sort
A lunch menu lists dishes whose names are jumbles of the same letters. Group them so the kitchen can print one row per base dish.
Given an array of strings, group all strings that are letter-shuffles of each other. Return the groups in any order; strings within a group can be in any order too.
Choose approach
eat
0tea
1tan
2ate
3nat
4bat
5Mapsignature → indices
empty
1/14
Narrationstep 1 of 14
Signature idea: words that sort to the same string are shuffles. Group by that signature.
Variables
i=—
signature=—
How to think about it
- Two words are shuffles iff their multisets of letters are equal.
- Pick a canonical key for each multiset — the simplest is the string sorted alphabetically.
- Hash each word by its signature; collect words into buckets.
- Return bucket values. Order doesn't matter.
- An O(n·k) alternative uses a 26-length frequency array as the key (no sort).
Edge cases to catch in interview
- Empty strings — all empties share the empty signature.
- Single character words — each unique letter is its own bucket.
- Very long words — the sort dominates per-word cost (k log k).
Examples
Example 1
[["bat"],["nat","tan"],["ate","eat","tea"]]{"strs":["eat","tea","tan","ate","nat","bat"],"values":["eat","tea","tan","ate","nat","bat"]}
Three signature buckets: 'abt', 'ant', 'aet'.
Example 2
[[""]]{"strs":[""],"values":[""]}
Empty string groups alone.
Example 3
[["a"]]{"strs":["a"],"values":["a"]}
Single word.
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed