|
|
@@ -2,17 +2,17 @@ import java.util.*;
|
|
2
|
2
|
|
|
3
|
3
|
public class Anagram {
|
|
4
|
4
|
|
|
5
|
|
- private AnagramSubject subject;
|
|
|
5
|
+ final private AnagramSubject anagramSubject;
|
|
6
|
6
|
|
|
7
|
7
|
public Anagram(String word) {
|
|
8
|
|
- subject = new AnagramSubject(word);
|
|
|
8
|
+ anagramSubject = new AnagramSubject(word);
|
|
9
|
9
|
}
|
|
10
|
10
|
|
|
11
|
11
|
public List<String> match(List<String> candidates) {
|
|
12
|
12
|
List<String> anagrams = new ArrayList<String>();
|
|
13
|
13
|
|
|
14
|
14
|
for (String candidate : candidates) {
|
|
15
|
|
- if (subject.anagramOf(candidate)) {
|
|
|
15
|
+ if (anagramSubject.anagramOf(candidate)) {
|
|
16
|
16
|
anagrams.add(candidate);
|
|
17
|
17
|
}
|
|
18
|
18
|
}
|
|
|
@@ -20,26 +20,26 @@ public class Anagram {
|
|
20
|
20
|
return anagrams;
|
|
21
|
21
|
}
|
|
22
|
22
|
|
|
23
|
|
- class AnagramSubject {
|
|
|
23
|
+ static final class AnagramSubject {
|
|
24
|
24
|
|
|
25
|
|
- private String subject;
|
|
26
|
|
- private char[] fingerprint;
|
|
|
25
|
+ private String word;
|
|
|
26
|
+ final private char[] fingerprint;
|
|
27
|
27
|
|
|
28
|
|
- public AnagramSubject(String word) {
|
|
29
|
|
- this.subject = word;
|
|
30
|
|
- this.fingerprint = canonicalize(word);
|
|
|
28
|
+ public AnagramSubject(String other) {
|
|
|
29
|
+ this.word = other;
|
|
|
30
|
+ this.fingerprint = canonicalize(other);
|
|
31
|
31
|
}
|
|
32
|
32
|
|
|
33
|
|
- public boolean anagramOf(String word) {
|
|
34
|
|
- return !duplicate(word) && Arrays.equals(fingerprint,(canonicalize(word)));
|
|
|
33
|
+ public boolean anagramOf(String other) {
|
|
|
34
|
+ return !duplicate(other) && Arrays.equals(fingerprint,canonicalize(other));
|
|
35
|
35
|
}
|
|
36
|
36
|
|
|
37
|
|
- private boolean duplicate(String word) {
|
|
38
|
|
- return subject.equalsIgnoreCase(word);
|
|
|
37
|
+ private boolean duplicate(String other) {
|
|
|
38
|
+ return word.equalsIgnoreCase(other);
|
|
39
|
39
|
}
|
|
40
|
40
|
|
|
41
|
|
- private char[] canonicalize(String word) {
|
|
42
|
|
- char[] chars = word.toLowerCase().toCharArray();
|
|
|
41
|
+ private char[] canonicalize(String other) {
|
|
|
42
|
+ char[] chars = other.toLowerCase().toCharArray();
|
|
43
|
43
|
Arrays.sort(chars);
|
|
44
|
44
|
return chars;
|
|
45
|
45
|
}
|