Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 798 Bytes

953.md

File metadata and controls

27 lines (25 loc) · 798 Bytes

953. Verifying an Alien Dictionary

Solution 1 (time O(n), space O(1))

class Solution(object):
    def isAlienSorted(self, words, order):
        """
        :type words: List[str]
        :type order: str
        :rtype: bool
        """
        d = {c: i for i, c in enumerate(order)}
        for i in range(1, len(words)):
            word1 = words[i - 1]
            word2 = words[i]
            ind = -1
            for j in range(min(len(word1), len(word2))):
                if word1[j] != word2[j]:
                    if d[word1[j]] > d[word2[j]]:
                        return False
                    ind = j
                    break
            if ind == -1 and len(word1) > len(word2):
                return False
        return True