-
Notifications
You must be signed in to change notification settings - Fork 118
/
Anagrams.java
55 lines (44 loc) · 1.43 KB
/
Anagrams.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package Algorithms;
import java.util.ArrayList;
import java.util.HashMap;
public class Anagrams {
public static void main(String[] args) {
Anagrams an = new Anagrams();
String[] strs = {"car","dog","god","rac","mon","bac","acr","mea","cab", "abd", "adb"};
//String[] strs = {"abc", "bac"};
System.out.println(an.anagrams(strs).toString());
}
private int getHash(int[] count) {
int hash = 0;
int a = 378551;
int b = 63689;
// int a = 777877;
// int b = 123451;
for (int num : count) {
hash = hash * a + num;
a = a * b;
}
return hash;
}
public ArrayList<String> anagrams(String[] strs) {
ArrayList<String> result = new ArrayList<String>();
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
for (String str : strs) {
int[] count = new int[26];
for (int i = 0; i < str.length(); i++) {
count[str.charAt(i) - 'a']++;
}
int hash = getHash(count);
if (!map.containsKey(hash)) {
map.put(hash, new ArrayList<String>());
}
map.get(hash).add(str);
}
for (ArrayList<String> tmp : map.values()) {
if (tmp.size() > 1) {
result.addAll(tmp);
}
}
return result;
}
}