49. Group Anagrams
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String s: strs) {
int[] counter = new int[26];
for(char ch: s.toCharArray()) {
counter[ch - 'a']++;
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 26; i++) {
sb.append("#").append(counter[i]);
}
String key = sb.toString();
if(!map.containsKey(key)) {
map.put(key, new ArrayList());
}
map.get(key).add(s);
}
return new ArrayList(map.values());
}
}