Leetcode 953. Verifying an Alien Dictionary

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 953. Verifying an Alien Dictionary相关的知识,希望对你有一定的参考价值。

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

1. Description

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 Dictionary的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 953. Verifying an Alien Dictionary

LeetCode --- 953. Verifying an Alien Dictionary 解题报告

leetcode 953. Verifying an Alien Dictionary & 949. Largest Time for Given Digits & 948. Bag

953. Verifying an Alien Dictionary

Leetcode 953. Verifying an Alien Dictionary

leetcode 每日一题 953. 验证外星语词典