Skip to content
CodeShare
All problems

Group Anagrams

Group together words that are anagrams of each other.

MediumStringsHash mapSorting

Given an array of strings words, group the words that are anagrams of each other.

You can return the groups in any order, and the words inside each group in any order.

Examples

Example 1
Input
words = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output
[["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
Example 2
Input
words = [""]
Output
[[""]]
Example 3
Input
words = ["a"]
Output
[["a"]]

Constraints

  • 1 <= words.length <= 10^4
  • 0 <= words[i].length <= 100
  • Words contain only lowercase English letters.