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, determine if the given words are sorted lexicographically in this alien language.
For example:
words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" Output: true Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
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.
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.
Could you provide an algorithm to solve this problem? What is the time and space complexity of your solution? Can you think of any edge cases?
The most straightforward approach is to iterate through the words
list and compare each pair of adjacent words to determine if they are in the correct order according to the given alien alphabet order
. This involves checking character by character until a difference is found or one of the words runs out of characters.
order
to its index (priority) in the alien alphabet.words
list from the first word to the second to last word.false
.false
.true
.def isAlienSorted_naive(words, order):
order_map = {char: i for i, char in enumerate(order)}
for i in range(len(words) - 1):
word1, word2 = words[i], words[i + 1]
min_len = min(len(word1), len(word2))
for j in range(min_len):
if order_map[word1[j]] > order_map[word2[j]]:
return False
elif order_map[word1[j]] < order_map[word2[j]]:
break # words are in order, check next pair
else:
# If all characters are same up to the min length, check length
if len(word1) > len(word2):
return False # word1 is longer, so out of order
return True
O(N * M), where N is the number of words and M is the average length of the words. In the worst case, we compare each character of each word.
O(1). The order_map
uses a fixed amount of space (26 characters).
The optimal approach is essentially the same as the naive approach, but with cleaner code and potentially minor optimizations. The core idea of comparing adjacent words based on the provided alien order remains unchanged.
compare_words(word1, word2, order_map)
to compare two words.compare_words
function, iterate through the characters of both words up to the length of the shorter word.word1
character has higher priority, return false
. If word2
character has higher priority, return true
.true
if word1
is shorter or equal in length to word2
, and false
otherwise.words
array and compare each pair of adjacent words using the compare_words
function.false
if any pair is out of order; otherwise, return true
.def isAlienSorted(words, order):
order_map = {char: i for i, char in enumerate(order)}
def compare_words(word1, word2, order_map):
min_len = min(len(word1), len(word2))
for i in range(min_len):
if order_map[word1[i]] > order_map[word2[i]]:
return False
elif order_map[word1[i]] < order_map[word2[i]]:
return True
return len(word1) <= len(word2)
for i in range(len(words) - 1):
if not compare_words(words[i], words[i + 1], order_map):
return False
return True
O(N * M), where N is the number of words and M is the average length of the words.
O(1). The order_map
uses a fixed amount of space (26 characters).
words
list: Should return true
because an empty list is considered sorted.words
list: Should return true
as a single element is always sorted.