class Solution {
public boolean compareWords(String word1, String word2, Map<Character, Integer> map) {
int ptr1 = 0, ptr2 = 0;
while (ptr1 < word1.length() && ptr2 < word2.length()) {
int order1 = map.get(word1.charAt(ptr1)), order2 = map.get(word2.charAt(ptr2));
if (order1 > order2) return false;
if (order1 < order2) return true;
ptr1++; ptr2++;
}
return ptr1 == word1.length();
}
public boolean isAlienSorted(String[] words, String order) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < order.length(); i++) map.put(order.charAt(i), i);
boolean ans = true;
for (int i = 0; i + 1 < words.length; i++) ans &= compareWords(words[i], words[i+1], map);
return ans;
}
}