Given an array of strings strs, group the anagrams together. You can return the answer in any order.
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Explanation:
"bat"."nat" and "tan" are anagrams as they can be rearranged to form each other."ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.Input: strs = [""]
Output: [[""]]
Input: strs = ["a"]
Output: [["a"]]
strs[i] consists of lowercase English letters.The first idea is to use the property that if two strings are anagrams of each other, their sorted form will be the same. By sorting each string and using it as a key, we can group the words that are anagrams together.
Instead of sorting, we can use the frequency of characters as a key. If two strings are anagrams, they will have the same frequency distribution of characters.