当前位置: 代码迷 >> 综合 >> Leetcode 953. Verifying an Alien Dictionary
  详细解决方案

Leetcode 953. Verifying an Alien Dictionary

热度:29   发布时间:2023-12-12 20:56:21.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 Falseelif dictionary[words[i][j]] < dictionary[words[i+1][j]]:breakelif dictionary[words[i][j]] > dictionary[words[i+1][j]]:return Falsereturn True

Reference

  1. https://leetcode.com/problems/verifying-an-alien-dictionary/
  相关解决方案