Created
November 15, 2019 17:22
-
-
Save forkercat/266896226c933460525cfdfc92c4bea8 to your computer and use it in GitHub Desktop.
953. Verifying an Alien Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isAlienSorted(String[] words, String order) { | |
if (words == null || words.length == 0 || order == null || order.length() == 0) { | |
return false; | |
} | |
Map<Character, Integer> orderRule = new HashMap<>(); | |
for (int i = 0; i < order.length(); ++i) { | |
char ch = order.charAt(i); | |
orderRule.put(ch, i); | |
} | |
for (int i = 0; i < words.length - 1; ++i) { | |
String s1 = words[i]; | |
String s2 = words[i + 1]; | |
int j = 0; | |
while (j < s1.length() && j < s2.length() && s1.charAt(j) == s2.charAt(j)) { | |
++j; | |
} | |
if (j < s1.length() && j < s2.length()) { | |
if (orderRule.get(s1.charAt(j)) > orderRule.get(s2.charAt(j))) { | |
return false; | |
} | |
} else { | |
if (j < s1.length() && j >= s2.length()) return false; // "abcdef" vs. "abc" | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment