Given an array of strings, group anagrams(相同字母异序词) together.
Example:
1 | Input: ["eat", "tea", "tan", "ate", "nat", "bat"], |
Note:
All inputs will be in lowercase.
The order of your output does not matter.
题解:
C++ unordered_map and counting sort
Use an unordered_map to group the strings by their sorted counterparts. Use the sorted string as the key and all anagram strings as the value.
1 | class Solution { |
Moreover, since the string only contains lower-case alphabets, we can sort them using counting sort to improve the time complexity.
1 | class Solution { |
1 | public class Solution { |