backbenchcode
← all problemsnext: Design an LRU Cache
#08MediumArrays & Strings · 58% acceptance

Group by Signature

Hash MapStringSorting

Given an array of strings, group the anagrams together. Two strings are anagrams if they contain the same letters in any order.

You bin samples by a normalized signature all the time. The trick here is choosing a good canonical key.

Examples

Input: ["eat","tea","tan","ate","nat","bat"]

Output: [["eat","tea","ate"],["tan","nat"],["bat"]]

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
7
8
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.