Leetcode 953. Verifying an Alien Dictionary (modified lexicographic)

953. Verifying an Alien Dictionary (modified lexicographic)

Hint : It's modified lexicographic comparation.


class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        n = len(words)
        if n < 2 :
            return True
        self.alienDict = {}
        for i in range(len(order)):
            self.alienDict[order[i]] = i
        
        for i in range(n-1):
            if not self.strLE(words[i], words[i+1]) :
                return False
        return True
    def strLE(self, strA, strB):
        n = min(len(strA), len(strB))
        for i in range(n):
            result = self.charCompare(strA[i], strB[i])
            if result > 0 :
                return False
            elif result == 0 :
                continue
            else :
                return True
        return len(strA) <= len(strB)
        
    def charCompare(self, charA, charB):
        if self.alienDict[charA] < self.alienDict[charB] :
            return -1
        elif self.alienDict[charA] > self.alienDict[charB] :
            return 1
        else :
            return 0