medium String

Group Anagrams

Write a function groupAnagrams that takes an array of strings and groups the anagrams together.

Function Signature

function groupAnagrams(strs);

Example 1:

Input: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Explanation: All strings that are anagrams are grouped together.

Example 2:

Input: strs = [""]
Output: [[""]]
Explanation: An empty string is considered an anagram of itself.

Example 3:

Input: strs = ["a"]
Output: [["a"]]
Explanation: A single character string is considered an anagram of itself.

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem