forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_953.java
74 lines (66 loc) · 2.47 KB
/
_953.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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class _953 {
public static class Solution1 {
public boolean isAlienSorted(String[] words, String order) {
if (words.length == 1) {
return true;
}
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < order.length(); i++) {
map.put(order.charAt(i), i);
}
for (int i = 0; i < words.length - 1; i++) {
String firstWord = words[i];
String secondWord = words[i + 1];
if (!sorted(firstWord, secondWord, map)) {
return false;
}
}
return true;
}
private boolean sorted(String firstWord, String secondWord, Map<Character, Integer> map) {
for (int i = 0; i < Math.min(firstWord.length(), secondWord.length()); i++) {
if (firstWord.charAt(i) == secondWord.charAt(i)) {
continue;
} else {
if (map.get(firstWord.charAt(i)) > map.get(secondWord.charAt(i))) {
return false;
} else {
return true;
}
}
}
return firstWord.length() <= secondWord.length();
}
}
public static class Solution2 {
public boolean isAlienSorted(String[] words, String order) {
String[] copy = Arrays.copyOf(words, words.length);
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
pos1 = order.indexOf(o1.charAt(i));
pos2 = order.indexOf(o2.charAt(i));
}
if (pos1 == pos2 && o1.length() != o2.length()) {
return o1.length() - o2.length();
}
return pos1 - pos2;
}
});
for (int i = 0; i < words.length; i++) {
if (!copy[i].equals(words[i])) {
return false;
}
}
return true;
}
}
}