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 lexicographically in this alien language.
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).
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
words[i]
and order
are English lowercase letters.This problem asks us to verify if a list of words is lexicographically sorted according to a given alien alphabet. The solution involves comparing pairs of consecutive words to check their lexicographical order based on the provided alphabet order.
Create a mapping: Construct a mapping from each character in the alien alphabet to its index. This allows for efficient comparison of characters based on their order in the alien alphabet. We can use a hash map (dictionary in Python) or an array for this purpose.
Iterate through word pairs: Iterate through consecutive pairs of words in the input list.
Compare characters: For each pair of words, compare their characters one by one, using the created mapping to determine the order.
Handle unequal length words: Special handling is needed when comparing words of unequal length. If a shorter word precedes a longer word and the shorter word is a prefix of the longer one (e.g., "apple" and "app"), then it violates the lexicographical order.
Return the result: If any pair of words violates the lexicographical order, return false
. Otherwise, return true
after checking all pairs.
Time Complexity: O(N*M), where N is the number of words and M is the maximum length of a word. This is because we iterate through each word once (O(N)) and potentially compare all characters in each word (O(M) in the worst case).
Space Complexity: O(1). The space used by the mapping is constant since the alphabet size is fixed (26 characters). We use a fixed amount of additional space for temporary variables irrespective of input size.
The Python code implements the approach efficiently using a dictionary for mapping and a concise loop structure:
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
m = {c: i for i, c in enumerate(order)} # Create character mapping
for i in range(len(words) - 1): # Iterate through word pairs
word1, word2 = words[i], words[i + 1]
min_len = min(len(word1), len(word2))
diff = False #Flag to indicate different char found
for j in range(min_len): #Compare chars
if m[word1[j]] < m[word2[j]]:
diff = True
break
elif m[word1[j]] > m[word2[j]]:
return False #Lexicographical order violated
if not diff and len(word1) > len(word2): #Handle unequal length
return False
return True #All pairs are lexicographically sorted
The other languages follow a similar approach with the data structures and control flow adjusted to their specific syntax. The core logic remains consistent across all implementations.