953 Verifying an Alien Dictionary 驗證外星語詞典
Description:
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.
Example:
Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '?', where '?' is defined as the blank character which is less than any other character (More info).
Note:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
All characters in words[i] and order are english lowercase letters.
題目描述:
某種外星語也使用英文小寫字母,但可能順序 order 不同。字母表的順序(order)是一些小寫字母的排列。
給定一組用外星語書寫的單詞 words,以及其字母表的順序 order,只有當給定的單詞在這種外星語中按字典序排列時,返回 true;否則,返回 false。
示例 :
示例 1:
輸入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
輸出:true
解釋:在該語言的字母表中,'h' 位于 'l' 之前,所以單詞序列是按字典序排列的。
示例 2:
輸入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
輸出:false
解釋:在該語言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此單詞序列不是按字典序排列的。
示例 3:
輸入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
輸出:false
解釋:當前三個字符 "app" 匹配時,第二個字符串相對短一些,然后根據詞典編纂規則 "apple" > "app",因為 'l' > '?',其中 '?' 是空白字符,定義為比任何其他字符都小(更多信息)。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
在 words[i] 和 order 中的所有字符都是英文小寫字母。
思路:
- 改寫比較器的方法, 按照 order的順序比較兩個字符串的長度和對應位置的大小
- 按照 order的順序重新給 words中的單詞賦值, 再比較兩個字符串的大小
時間復雜度O(mn), 空間復雜度O(1), 其中 n為 words數組的大小, m為數組中的每一個字符串的長度
代碼:
C++:
class Solution
{
public:
bool isAlienSorted(vector<string>& words, string order)
{
int index[128] = {0};
for (int i = 0; i < 26; i++) index[order[i]] = i + 'a';
for (int i = 0; i < words.size(); i++) for (int j = 0; j < words[i].size(); j++) words[i][j] = index[words[i][j]];
for (int i = 1; i < words.size(); i++) if (words[i - 1] > words[i]) return false;
return true;
}
};
Java:
class Solution {
public boolean isAlienSorted(String[] words, String order) {
Comparator<String> comparator = (a, b) -> {
for (int i = 0; i < Math.min(a.length(), b.length()); i++) if (a.charAt(i) != b.charAt(i)) return order.indexOf(a.charAt(i)) - order.indexOf(b.charAt(i));
return a.length() - b.length();
};
for (int i = 1; i < words.length; i++) if (comparator.compare(words[i - 1], words[i]) > 0) return false;
return true;
}
}
Python:
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
return words == sorted(words, key=lambda word: [order.index(x) for x in word])