Leetcode 990 Solution

This article provides solution to leetcode question 990 (verifying-an-alien-dictionary)

https://leetcode.com/problems/verifying-an-alien-dictionary

Solution

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        order_index = {c: i for i, c in enumerate(order)}

        for i in range(len(words) - 1):
            word1 = words[i]
            word2 = words[i + 1]

            for j in range(min(len(word1), len(word2))):
                if order_index[word1[j]] < order_index[word2[j]]:
                    break
                elif order_index[word1[j]] > order_index[word2[j]]:
                    return False
            else:
                if len(word1) > len(word2):
                    return False

        return True