-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (29 loc) · 1.02 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// https://leetcode.com/problems/group-anagrams
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
int indexes[] = new int[strs.length];
Arrays.fill(indexes, -1);
List<List<String>> list = new ArrayList<>();
String newStrs[] = new String[strs.length];
for(int i=0;i<strs.length;i++){
char arr[] = strs[i].toCharArray();
Arrays.sort(arr);
newStrs[i] = new String(arr);
}
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<strs.length;i++){
if(map.containsKey(newStrs[i])){
list.get(map.get(newStrs[i])).add(strs[i]);
}else{
map.put(newStrs[i], list.size());
List<String> temp = new ArrayList<>();
temp.add(strs[i]);
list.add(temp);
}
}
for(List<String> temp : list){
Collections.sort(temp);
}
return list;
}
}