问题
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 lower-case English letters.
思考
只含有小写英文字符,所以只需要size为26的数组就可以统计出现的字符的个数, 然后按照顺序转换为String, 将String 作为map的key 来储存原始字符串. 最后将map的values 输出为List.
方法1
时间复杂度O(mn).
空间复杂度O(mn).
m: 字符串的个数
n:最长字符串的长度
class Solution