Group Anagrams

Given an array of strings strs, group the anagrams together.

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all original letters exactly once.

Return a list of groups, where each group contains strings from strs that are anagrams of one another. You may return the groups in any order, and the strings within each group may also be in any order.

Example 1
Inputstrs = ["eat","tea","tan","ate","nat","bat"]
Output[["bat"],["nat","tan"],["ate","eat","tea"]]
The strings "eat", "tea", and "ate" are anagrams, as are "tan" and "nat", while "bat" forms its own group.
Example 2
Inputstrs = [""]
Output[[""]]
The only string is the empty string, so it forms a single anagram group by itself.

Constraints

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

Asked at 90 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate