-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAnagrams (pyemma)
More file actions
32 lines (28 loc) · 991 Bytes
/
Anagrams (pyemma)
File metadata and controls
32 lines (28 loc) · 991 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
27
28
29
30
31
32
import java.util.*;
public class Anagrams {
public ArrayList<String> anagrams(String[] strs) {
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> res = new ArrayList<String>();
if(strs.length == 0) return res;
for(int i = 0; i < strs.length; i++) {
char[] chs = strs[i].toCharArray();
Arrays.sort(chs); // returns void!
String str = new String(chs);
if(map.containsKey(str) == false) {
ArrayList<String> arr = new ArrayList<String>();
arr.add(strs[i]);
map.put(str, arr);
}
else {
map.get(str).add(strs[i]);
}
}
for(String str : map.keySet()) {
if(map.get(str).size() > 1) {
for(String s : map.get(str))
res.add(s);
}
}
return res;
}
}