CodeSpeek

Group Anagrams

Medium · Arrays & Hashing

Given a list of strings, group the strings that are anagrams of one another into the same sublist. Two strings are anagrams if one can be rearranged (by reordering its characters) to form the other. Return the groups as a list of lists; the groups can be returned in any order, and the strings within each group can be in any order.

Examples

Input:  strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Why:    Words that are anagrams of each other (same letters rearranged) are grouped together; the order of groups and order within groups does not matter.
Input:  strs = [""]
Output: [[""]]
Why:    A single empty string forms its own group.
Input:  strs = ["a"]
Output: [["a"]]
Why:    A single character forms its own group.

Constraints

1 <= strs.length <= 10^4, 0 <= strs[i].length <= 100, strs[i] consists of lowercase English letters only

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Group Anagrams

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Group Anagrams. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.