Medium
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.
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”]]
Constraints:
1 <= strs.length <= 104
0 <= strs[i].length <= 100
strs[i]
consists of lowercase English letters.To solve the “Group Anagrams” problem, you can use a hash table to group the anagrams together based on their sorted representations. Here are the steps to solve the problem:
strs
.from collections import defaultdict
class Solution:
def groupAnagrams(self, strs):
# Initialize a dictionary to store groups of anagrams
groups = defaultdict(list)
# Group anagrams based on their sorted representations
for word in strs:
sorted_word = ''.join(sorted(word))
groups[sorted_word].append(word)
# Return the groups of anagrams
return list(groups.values())
# Example Usage:
solution = Solution()
# Example 1:
strs1 = ["eat","tea","tan","ate","nat","bat"]
print(solution.groupAnagrams(strs1)) # Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
# Example 2:
strs2 = [""]
print(solution.groupAnagrams(strs2)) # Output: [[""]]
# Example 3:
strs3 = ["a"]
print(solution.groupAnagrams(strs3)) # Output: [["a"]]
This code defines a Solution
class with a groupAnagrams
method to group the anagrams together. The example usage demonstrates how to create an instance of the Solution
class and call the groupAnagrams
method with different input arrays of strings.