加载中...
953-验证外星语词典(Verifying an Alien Dictionary)
发表于:2021-12-03 | 分类: 简单
字数统计: 525 | 阅读时长: 2分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/verifying-an-alien-dictionary

英文原文

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

 

Example 1:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • All characters in words[i] and order are English lowercase letters.

中文题目

某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false

 

示例 1:

输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。

示例 2:

输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出:false
解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。

示例 3:

输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出:false
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。

 

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • 在 words[i] 和 order 中的所有字符都是英文小写字母。

通过代码

官方题解

方法一: 检查相邻单词

思路

只有每对相邻单词都是有序的,那么整个 words 才是有序的。因为有序性是可以传递的,例如,a <= bb <= c 可以推出 a <= c

算法

检查相邻单词 ab 是否满足 a <= b

为了检查相邻单词 ab 是否满足 a <= b,只需要检查它们第一个不同的字母就可以了。例如,对于"applying""apples",第一个不同的字母是 ye。之后只需要比较这两个字母在 order 中的下标就可以了。

还需要考虑两个单词长度不等的情况。例如,当比较 "app""apply" 的时候,前三个字母都是相等的,但 "app""apply" 更短,所以满足 a <= b

[solution1-Java]
class Solution { public boolean isAlienSorted(String[] words, String order) { int[] index = new int[26]; for (int i = 0; i < order.length(); ++i) index[order.charAt(i) - 'a'] = i; search: for (int i = 0; i < words.length - 1; ++i) { String word1 = words[i]; String word2 = words[i+1]; // Find the first difference word1[k] != word2[k]. for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) { if (word1.charAt(k) != word2.charAt(k)) { // If they compare badly, it's not sorted. if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a']) return false; continue search; } } // If we didn't find a first difference, the // words are like ("app", "apple"). if (word1.length() > word2.length()) return false; } return true; } }
[solution1-Python]
class Solution(object): def isAlienSorted(self, words, order): order_index = {c: i for i, c in enumerate(order)} for i in xrange(len(words) - 1): word1 = words[i] word2 = words[i+1] # Find the first difference word1[k] != word2[k]. for k in xrange(min(len(word1), len(word2))): # If they compare badly, it's not sorted. if word1[k] != word2[k]: if order_index[word1[k]] > order_index[word2[k]]: return False break else: # If we didn't find a first difference, the # words are like ("app", "apple"). if len(word1) > len(word2): return False return True

复杂度分析

  • 时间复杂度: $O(C)$,其中 $C$ 是 words 中单词总长度和。

  • 空间复杂度: $O(1)$。

统计信息

通过次数 提交次数 AC比率
15133 27457 55.1%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
952-按公因数计算最大组件大小(Largest Component Size by Common Factor)
下一篇:
955-删列造序 II(Delete Columns to Make Sorted II)
本文目录
本文目录