LeetCode 953 - Verifying an Alien Dictionary
Input: Words = ['a', 'aa', 'cb', 'bc'] Order = ['a', 'c', ‘b']
Output: True
Input: Words = ['a', 'aa', 'cb', ‘bc'] Order = ['c', 'a', 'b']
Output: False (cb -> a -> aa -> bc)
意思就是给⼀一组words和⼀一个character的order list,请问words⾥里里⾯面是不不
是按照 order list⾥里里⾯面的order排序。 Return True or False
2. [“ab”, “a”] ⾮非范围内 的字⺟母?
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/Check%20Words%20Order/CheckWordsOrder.java
- not exist words
Input: Words = ['a', 'aa', 'cb', 'bc'] Order = ['a', 'c', ‘b']
Output: True
Input: Words = ['a', 'aa', 'cb', ‘bc'] Order = ['c', 'a', 'b']
Output: False (cb -> a -> aa -> bc)
意思就是给⼀一组words和⼀一个character的order list,请问words⾥里里⾯面是不不
是按照 order list⾥里里⾯面的order排序。 Return True or False
2. [“ab”, “a”] ⾮非范围内 的字⺟母?
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/Check%20Words%20Order/CheckWordsOrder.java
private boolean check(String wa, String wb, Map<Character, Integer> dict) {
int len = Math.min(wa.length(), wb.length());
for (int i = 0; i < len; i ++) {
if (dict.get(wa.charAt(i)) < dict.get(wb.charAt(i))) return true;
if (dict.get(wa.charAt(i)) > dict.get(wb.charAt(i))) return false;
}
if (wa.length() <= wb.length()) return true;
else return false;
}
private boolean checkOrder(String[] words, char[] diction) {
HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
for (int i = 0; i < diction.length; i ++)
dict.put(diction[i], i);
for (int i = 0; i < words.length - 1; i ++)
if (!check(words[i], words[i + 1], dict))
return false;
return true;
}
- not exist words