Alien Dictionary (Mutation)
Alien Dictionary (Leetcode 269 mutation) (Correctness Unchecked)
Simplify version of Leetcode 269
Question
Solution
public boolean orderMatch(String[] words, char[] ordering) {
Map<Character,Integer> hm = new HashMap<>();
for(int i=0;i<ordering.length;i++) hm.put(ordering[i],i);
for(int i=1;i<words.length;i++) {
String s1 = words[i-1];
String s2 = words[i];
int j=0;
while(j<s1.length() && j<s2.length() && s1.charAt(j)==s2.charAt(j)) j++; //skip the prefix identical characters
if(j<s1.length() && j<s2.length()) {
if(hm.get(s1.charAt(j)>hm.get(s2.charAt(j)))) return false;
}
}
return true;
}Last updated