-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path49.java
More file actions
20 lines (19 loc) · 693 Bytes
/
49.java
File metadata and controls
20 lines (19 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<List<String>>();
HashMap<String, List<String>> h = new HashMap<>();
for(int i=0; i < strs.length;i++){
char[] chars = strs[i].toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
if(h.get(sorted) == null){
h.put(sorted, new ArrayList<>());
}
h.get(sorted).add(strs[i]);
}
for (Map.Entry<String, List<String>> entry : h.entrySet()) {
res.add(entry.getValue());
}
return res;
}
}