文章作者: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
- https://leetcode.com/problems/verifying-an-alien-dictionary/