forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerifyAnAlienDictionary.java
33 lines (29 loc) · 1.25 KB
/
VerifyAnAlienDictionary.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
import java.util.Comparator;
public class VerifyAnAlienDictionary {
public boolean isAlienSorted(String[] words, String order) {
AlienLanguageComparator alienLanguageComparator = new AlienLanguageComparator(order);
for (int i = 1 ; i < words.length ; i++) {
if (alienLanguageComparator.compare(words[i - 1], words[i]) > 0) {
return false;
}
}
return true;
}
private static class AlienLanguageComparator implements Comparator<String> {
final int[] characterOrder = new int[26];
AlienLanguageComparator(String order) {
for (int index = 0 ; index < 26 ; index++) {
characterOrder[order.charAt(index) - 'a'] = index;
}
}
@Override
public int compare(String s1, String s2) {
if (s1.length() == s2.length() && s1.equals(s2)) return 0;
for (int i = 0, j = 0 ; i < s1.length() && j < s2.length() ; i++, j++) {
if (characterOrder[s1.charAt(i) - 'a'] < characterOrder[s2.charAt(j) - 'a']) return -1;
else if (characterOrder[s1.charAt(i) - 'a'] > characterOrder[s2.charAt(j) - 'a']) return 1;
}
return s1.length() - s2.length();
}
}
}