Group Anagrams

Medium~25 min

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

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

Examples

Example 1
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Explanation: There are 3 groups: "eat"/"tea"/"ate" are anagrams, "tan"/"nat" are anagrams, and "bat" stands alone.
Example 2
Input: strs = [""]
Output: [[""]]
Explanation: A single empty string forms its own group.
Example 3
Input: strs = ["a"]
Output: [["a"]]
Explanation: A single string forms its own group.

Constraints

  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters
  • Expected time complexity: O(n*k)
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run