952.验证外星语词典
程序员小航
2022-05-22 11:46
题目
题解
只需要每两个字段比较一下是否字典序即可
class Solution {
public boolean isAlienSorted(String[] words, String order) {
for (int i = 0; i < words.length - 1; i++) {
if (!isAlienSorted(words[i], words[i + 1], order)) {
return false;
}
}
return true;
}
public boolean isAlienSorted(String word1, String word2, String order) {
int minL = Math.min(word1.length(), word2.length());
for (int i = 0; i < minL; i++) {
int indexOf1 = order.indexOf(word1.charAt(i));
int indexOf2 = order.indexOf(word2.charAt(i));
if (indexOf1 == indexOf2) {
continue;
}
return indexOf1 < indexOf2;
}
return word1.length() <= word2.length();
}
}
-
评论