-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathVerifyingAlienDictionary953.java
75 lines (56 loc) · 1.76 KB
/
VerifyingAlienDictionary953.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package easy;
import java.util.HashMap;
import java.util.Map;
/*
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1],
hence the sequence is unsorted.
*/
public class VerifyingAlienDictionary953 {
public static boolean isAlienSorted(String[] words, String order) {
if (words.length == 1)
return true;
for (int i = 0; i < words.length - 1; i++) {
boolean checkCurrentOrder = checkTwoWords(words[i], words[i + 1], order);
if (checkCurrentOrder == false) {
return false;
}
}
return true;
}
private static boolean checkTwoWords(String current, String next, String order) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 1; i <= 26; i++) {
map.put(order.charAt(i - 1), i);
}
for (int idx = 0; idx < current.length(); idx++) {
char ch1 = current.charAt(idx);
// if not matched yet but next string is traversed already
if (idx == next.length())
return false;
char ch2 = next.charAt(idx);
int order1 = map.get(ch1);
int order2 = map.get(ch2);
if (order1 == order2)
continue;
else if (order1 > order2)
return false;
else
return true;
}
return true;
}
public static void main(String[] args) {
String[] words2 = { "hello", "leetcode" };
String order2 = "hlabcdefgijkmnopqrstuvwxyz";
System.out.println(isAlienSorted(words2, order2));
String[] words = { "word", "world", "row" };
String order = "worldabcefghijkmnpqstuvxyz";
System.out.println(isAlienSorted(words, order));
String[] words3 = { "apple", "app" };
// String[] words3 = { "apap", "app" };
String order3 = "abcdefghijklmnopqrstuvwxyz";
System.out.println(isAlienSorted(words3, order3));
}
}