Pattern visualizer
Group Anagrams
Every anagram shares the same sorted-letter key (e.g. "eat" and "tea" both sort to "aet"). Bucket each word by that key in a map, then return the buckets. Reach for it whenever grouping strings by a shared signature. Animated on: ["eat","tea","tan","ate","nat","bat"] — group words that are anagrams of each other.
Strings
step 1 / 8
eat
[0]tea
[1]tan
[2]ate
[3]nat
[4]bat
[5]line 2
For each word, sort its letters into a key and drop it into that key's bucket.
Pseudocode
1FUNCTION groupAnagrams(words):2 make an empty lookup table (sorted-letters key -> list of words)3 FOR each word in words:4 key = the word's letters sorted and joined into a string5 IF key is not in the table: store key -> an empty list6 add word to the list stored for key7 END FOR8 RETURN all the lists in the table9END FUNCTION
← / → step · space play · Home restart