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.
Example 1:
words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
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:
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.
Write a function to solve this problem. The function should take a list of strings words
and a string order
as input and return a boolean value indicating whether the words are sorted lexicographically according to the given order.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given words that are supposed to be in order based on a strange alien alphabet, and we want to check if they really are. The brute force way to do this is to directly compare each pair of adjacent words to make sure they follow the alphabet's rules.
Here's how the algorithm would work step-by-step:
def is_alien_sorted(words, order):
order_map = {char: index for index, char in enumerate(order)}
def compare_words(word1, word2):
minimum_length = min(len(word1), len(word2))
# Compare letters up to min length
for index in range(minimum_length):
char1 = word1[index]
char2 = word2[index]
if char1 != char2:
# Check alien alphabet order
if order_map[char1] > order_map[char2]:
return False
return True
# Handle cases where one word is a prefix of the other
if len(word1) > len(word2):
return False
return True
# Iterate through each pair of adjacent words
for i in range(len(words) - 1):
# Verify that the current word comes before the next
if not compare_words(words[i], words[i + 1]):
return False
return True
We need to figure out if a list of words is sorted according to a strange alien alphabet. The key idea is to check pairs of adjacent words to see if they're in the correct order based on the given alien alphabet.
Here's how the algorithm would work step-by-step:
def isAlienSorted(words, order):
alien_order_map = {char: index for index, char in enumerate(order)}
# Compare each adjacent pair of words
for i in range(len(words) - 1):
word1 = words[i]
word2 = words[i+1]
min_length = min(len(word1), len(word2))
# Iterate through letters until a diff is found
for j in range(min_length):
if word1[j] != word2[j]:
# Check if order is incorrect
if alien_order_map[word1[j]] > alien_order_map[word2[j]]:
return False
break
else:
# Check if word1 is a prefix of word2
# If word1 is longer, then the order is wrong
if len(word1) > len(word2):
return False
return True
Case | How to Handle |
---|---|
words is null or empty | Return true if words is null or empty, as an empty set of words is considered sorted. |
order is null or empty | If order is null or empty and words is not, words can only be sorted if it contains only one word; otherwise it is false. |
words contains an empty string | An empty string is considered lexicographically smaller than any non-empty string, so it should be handled correctly in the comparison. |
order contains duplicate characters | The order string should be considered invalid, and the program should likely return an error or false. |
words are all identical | Identical words should be considered sorted, so the comparison function should return true. |
One word is a prefix of another (e.g., 'apple' and 'app') | The shorter word should come before the longer word; if the shorter word is lexicographically *after* the longer word, return false. |
Large input size (many words and/or long words) | The solution should iterate efficiently through the words and characters without excessive memory usage, ideally using a dictionary (hashmap) for character lookups in O(1) time. |
Words contain characters not in the order string | These characters are considered invalid and should result in a false return or error condition, as the ordering cannot be determined. |