美文网首页Leetcode
Leetcode 953. Verifying an Alien

Leetcode 953. Verifying an Alien

作者: SnailTyan | 来源:发表于2021-08-31 08:36 被阅读0次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Verifying an Alien Dictionary

    2. Solution

    解析:Version 1,构造顺序字典,通过索引的大小来判断顺序,每个单词都跟后一个单词逐字母比较,如果字母不同,且字母顺序满足条件,直接进行下一轮比较,如果顺序不对,直接返回False,如果字母都相等,但当前单词长度更大,也直接返回False

    • Version 1
    class Solution:
        def isAlienSorted(self, words: List[str], order: str) -> bool:
            dictionary = {k: v for v, k in enumerate(order)}
            for i in range(len(words) - 1):
                for j in range(len(words[i])):
                    if j == len(words[i+1]):
                        return False
                    elif dictionary[words[i][j]] < dictionary[words[i+1][j]]:
                        break
                    elif dictionary[words[i][j]] > dictionary[words[i+1][j]]:
                        return False
            return True
    

    Reference

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

    相关文章

      网友评论

        本文标题:Leetcode 953. Verifying an Alien

        本文链接:https://www.haomeiwen.com/subject/xwabvltx.html