class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
m = collections.defaultdict(list)
for s in strs:
# m[''.join(sorted(s))].append(s)
cnt = [0] * 26
for c in s:
cnt[ord(c) - ord('a')] += 1
m[tuple(cnt)].append(s)
return m.values()
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] cs = str.toCharArray();
Arrays.sort(cs);
String keyStr = String.valueOf(cs);
if (map.containsKey(keyStr)) {
map.get(keyStr).add(str);
} else {
map.put(keyStr, new ArrayList<>());
map.get(keyStr).add(str);
}
}
List<List<String>> res = new ArrayList<>();
for (String str : map.keySet()) {
res.add(map.get(str));
}
return res;
}
}