-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy path49.Group-Anagrams.java
More file actions
26 lines (25 loc) · 955 Bytes
/
49.Group-Anagrams.java
File metadata and controls
26 lines (25 loc) · 955 Bytes
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
//https://leetcode.com/problems/group-anagrams/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
ArrayList<List<String>> answer = new ArrayList<List<String>>();
HashMap<String, ArrayList<String>> maps = new HashMap<String, ArrayList<String>>();
for(int i=0; i<strs.length; i++){
char charArray[] = strs[i].toCharArray();
Arrays.sort(charArray);
String str = new String(charArray);
if(maps.get(str)==null){
ArrayList<String> alist = new ArrayList<String>();
alist.add(strs[i]);
maps.put(str, alist);
}else{
ArrayList<String> existList = maps.get(str);
existList.add(strs[i]);
maps.put(str, existList);
}
}
for(String key: maps.keySet()){
answer.add(maps.get(key));
}
return answer;
}
}