Group Anagrams

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

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

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, 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: The strings "eat", "tea", and "ate" are anagrams. "tan" and "nat" are anagrams. "bat" has no anagram in the list.

Example 2:

Input: strs = [""]

Output: [[""]]

Explanation: The 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.
Source: Hash Maps pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle