|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/)** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「排序」、「模拟」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +某种外星语也使用英文小写字母,但可能顺序 `order` 不同。字母表的顺序(`order`)是一些小写字母的排列。 |
| 10 | + |
| 11 | +给定一组用外星语书写的单词 `words`,以及其字母表的顺序 `order`,只有当给定的单词在这种外星语中按字典序排列时,返回 `true`;否则,返回 `false`。 |
| 12 | + |
| 13 | +示例 1: |
| 14 | +``` |
| 15 | +输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" |
| 16 | + |
| 17 | +输出:true |
| 18 | + |
| 19 | +解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。 |
| 20 | +``` |
| 21 | +示例 2: |
| 22 | +``` |
| 23 | +输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz" |
| 24 | + |
| 25 | +输出:false |
| 26 | + |
| 27 | +解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。 |
| 28 | +``` |
| 29 | +示例 3: |
| 30 | +``` |
| 31 | +输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz" |
| 32 | + |
| 33 | +输出:false |
| 34 | + |
| 35 | +解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。 |
| 36 | +``` |
| 37 | + |
| 38 | +提示: |
| 39 | +* 1ドル <= words.length <= 100$ |
| 40 | +* 1ドル <= words[i].length <= 20$ |
| 41 | +* $order.length == 26$ |
| 42 | +* 在 `words[i]` 和 `order` 中的所有字符都是英文小写字母。 |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### 自定义排序 |
| 47 | + |
| 48 | +为了快速判断某两个字符在字典序的前后关系,先使用一个大小与字符集相等的数组对 `order` 进行转存。 |
| 49 | + |
| 50 | +然后对 `words` 进行拷贝复制得到 `clone`,并执行自定义排序,最后根据排序前后顺序是否相等来返回答案。 |
| 51 | + |
| 52 | +代码: |
| 53 | +```Java |
| 54 | +class Solution { |
| 55 | + public boolean isAlienSorted(String[] words, String order) { |
| 56 | + int[] ord = new int[26]; |
| 57 | + for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i; |
| 58 | + String[] clone = words.clone(); |
| 59 | + Arrays.sort(clone, (a, b)->{ |
| 60 | + int n = a.length(), m = b.length(); |
| 61 | + int i = 0, j = 0; |
| 62 | + while (i < n && j < m) { |
| 63 | + int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a'; |
| 64 | + if (c1 != c2) return ord[c1] - ord[c2]; |
| 65 | + i++; j++; |
| 66 | + } |
| 67 | + if (i < n) return 1; |
| 68 | + if (j < m) return -1; |
| 69 | + return 0; |
| 70 | + }); |
| 71 | + int n = words.length; |
| 72 | + for (int i = 0; i < n; i++) { |
| 73 | + if (!clone[i].equals(words[i])) return false; |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | +* 时间复杂度:$O(n\log{n})$ |
| 80 | +* 空间复杂度:$O(\log{n})$ |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +### 模拟 |
| 85 | + |
| 86 | +更近一步,我们无须对整个数组进行排序,只需要将「自定义排序」中的规则抽出来,判断相邻两个字符串是满足字典序排序即可。 |
| 87 | + |
| 88 | +代码: |
| 89 | +```Java |
| 90 | +class Solution { |
| 91 | + int[] ord = new int[26]; |
| 92 | + int check(String a, String b) { |
| 93 | + int n = a.length(), m = b.length(); |
| 94 | + int i = 0, j = 0; |
| 95 | + while (i < n && j < m) { |
| 96 | + int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a'; |
| 97 | + if (c1 != c2) return ord[c1] - ord[c2]; |
| 98 | + i++; j++; |
| 99 | + } |
| 100 | + if (i < n) return 1; |
| 101 | + if (j < m) return -1; |
| 102 | + return 0; |
| 103 | + } |
| 104 | + public boolean isAlienSorted(String[] words, String order) { |
| 105 | + for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i; |
| 106 | + int n = words.length; |
| 107 | + for (int i = 1; i < n; i++) { |
| 108 | + if (check(words[i - 1], words[i]) > 0) return false; |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | +* 时间复杂度:$O(n)$ |
| 115 | +* 空间复杂度:$O(C)$ |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +### 最后 |
| 120 | + |
| 121 | +这是我们「刷穿 LeetCode」系列文章的第 `No.953` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 122 | + |
| 123 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 124 | + |
| 125 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 126 | + |
| 127 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 128 | + |
0 commit comments