LeetCode: Anagrams

LeetCode: Anagrams

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
public class Anagrams {
public List<String> anagrams(String[] strs) {
List<String> result = new ArrayList<String>();
if (strs == null || strs.length < 2) {
return result;
}
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
for (int i = 0; i < strs.length; i++) {
String s = strs[i];
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = String.valueOf(chars);
if (map.containsKey(key)) {
map.get(key).add(i);
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(key, list);
}
}
for (List<Integer> list : map.values()) {
if (list.size() > 1) {
for (int i : list) {
result.add(strs[i]);
}
}
}
return result;
}
}