LeetCode 953 Verifying an Alien Dictionary (Python)

Posted by 小明MaxMing on April 13, 2021

题目

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 lexicographicaly in this alien language.

解题思路

将字母表存到字典中,相邻的两个字符串依次比较,判断是否符合要求,要处理两个单词一个是另外一个的前缀

代码

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        dic = {c: i for i, c in enumerate(order)}
        for i in range(len(words) - 1):
            word1 = words[i]
            word2 = words[i + 1]
            for k in range(min(len(word1), len(word2))):
                if word1[k] != word2[k]:
                    if dic[word1[k]] > dic[word2[k]]:
                        return False
                    break
            else:
                if len(word1) > len(word2):
                    return False
        return True

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅