跳至主要內容

953. 验证外星语词典


953. 验证外星语词典

🟢   🔖  数组 哈希表 字符串  🔗 力扣open in new window LeetCodeopen in new window

题目

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 infoopen in new window).

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' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息open in new window)。

提示:

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

解题思路

  1. 构建自定义顺序的映射表

    • order 转换为一个哈希表 orderMap,其中键为字母,值为其在 order 中的索引。
    • 通过 orderMap 快速查询每个字母的优先级。
  2. 定义比较函数

    • 编写函数 compare(word1, word2) 比较两个单词:
      • 按照字母顺序逐位比较:
        • 如果 word1[i] 的优先级小于 word2[i],则 word1 小于 word2
        • 如果 word1[i] 的优先级大于 word2[i],则 word1 大于 word2
        • 如果相等,则继续比较下一位。
      • 如果比较到末尾且没有确定大小关系,短单词优先(例如 "apple" 小于 "apples")。
  3. 逐对比较

    • 遍历 words 的相邻单词,调用 compare 检查是否满足顺序。
    • 如果发现任意一对不满足条件,直接返回 false
    • 如果所有相邻单词都满足条件,则返回 true

复杂度分析

  • 时间复杂度: O(n * m),其中 n 是单词数组的长度,m 是单个单词的平均长度。

    • 构建 orderMap 的时间复杂度为 O(26),常数时间。
    • 比较相邻单词需要最多 O(m) 的时间。
    • 总共进行 n-1 次比较,因此整体复杂度为 O(n * m)
  • 空间复杂度: O(1),只使用了常量级的额外空间(orderMap 占用固定空间)。

代码

/**
 * @param {string[]} words
 * @param {string} order
 * @return {boolean}
 */
var isAlienSorted = function (words, order) {
	let orderMap = new Map();
	order.split('').forEach((char, i) => orderMap.set(char, i));

	const compare = (word1, word2) => {
		const len1 = word1.length;
		const len2 = word2.length;
		let i = 0;
		while (i < len1 && i < len2) {
			if (orderMap.get(word1[i]) < orderMap.get(word2[i])) {
				return true;
			} else if (orderMap.get(word1[i]) > orderMap.get(word2[i])) {
				return false;
			} else {
				i++;
			}
		}
		return i == len1;
	};

	for (let i = 1; i < words.length; i++) {
		if (!compare(words[i - 1], words[i])) return false;
	}
	return true;
};