Challenge Statement

  • 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, typically using all the original letters exactly once.
  • This challenge corresponds to LeetCode #49.

Constraints

  • 1 <= len(strs) <= 104
  • 0 <= len(strs[i]) <= 100
  • strs[i] consists of lowercase English letters.

Example 1:

Input: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]

Output: [["bat"],["nat", "tan"],["ate", "eat", "tea"]]

Example 2:

Input: strs = [""]

Output: [[""]]

Example 3:

Input: strs = ["a"]

Output: [["a"]]

Solution

Below is my solution and some test cases. This solution has a linear time complexity O(m+n) and a linear space complexity O(m), where m is the length of the input list strs and n is the max length of the strings in strs.