953. Verifying an Alien Dictionary - cocoder39/coco39_LC GitHub Wiki
953. Verifying an Alien Dictionary
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
order_of_letters = {}
for i, ch in enumerate(order):
order_of_letters[ch] = i
word_count = len(words)
for i in range(1, word_count):
if not self.isSorted(words[i-1], words[i], order_of_letters):
return False
return True
def isSorted(self, word1: str, word2: str, order_of_letters: dict) -> bool:
word1_len, word2_len = len(word1), len(word2)
for i in range(min(word1_len, word2_len)):
ch1, ch2 = word1[i], word2[i]
if order_of_letters[ch1] < order_of_letters[ch2]:
return True
elif order_of_letters[ch1] > order_of_letters[ch2]:
return False
return word1_len <= word2_len