953. 验证外星语词典 : 简单自定义排序模拟题

题目描述

这是 LeetCode 上的 953. 验证外星语词典 ,难度为 简单

Tag : 「排序」、「模拟」

某种外星语也使用英文小写字母,但可能顺序 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 < = w o r d s . l e n g t h < = 100 1 <= words.length <= 100
  • 1 < = w o r d s [ i ] . l e n g t h < = 20 1 <= words[i].length <= 20
  • o r d e r . l e n g t h = = 26 order.length == 26
  • 在 words[i] 和 order 中的所有字符都是英文小写字母。

自定义排序

为了快速判断某两个字符在字典序的前后关系,先使用一个大小与字符集相等的数组对 order 进行转存。

然后对 words 进行拷贝复制得到 clone,并执行自定义排序,最后根据排序前后顺序是否相等来返回答案。

代码:

class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        int[] ord = new int[26];
        for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i;
        String[] clone = words.clone();
        Arrays.sort(clone, (a, b)->{
            int n = a.length(), m = b.length();
            int i = 0, j = 0;
            while (i < n && j < m) {
                int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a';
                if (c1 != c2) return ord[c1] - ord[c2];
                i++; j++;
            }
            if (i < n) return 1;
            if (j < m) return -1;
            return 0;
        });
        int n = words.length;
        for (int i = 0; i < n; i++) {
            if (!clone[i].equals(words[i])) return false;
        }
        return true;
    }
}
复制代码
  • 时间复杂度: O ( n log n ) O(n\log{n})
  • 空间复杂度: O ( log n ) O(\log{n})

模拟

更近一步,我们无须对整个数组进行排序,只需要将「自定义排序」中的规则抽出来,判断相邻两个字符串是满足字典序排序即可。

代码:

class Solution {
    int[] ord = new int[26];
    int check(String a, String b) {
        int n = a.length(), m = b.length();
        int i = 0, j = 0;
        while (i < n && j < m) {
            int c1 = a.charAt(i) - 'a', c2 = b.charAt(j) - 'a';
            if (c1 != c2) return ord[c1] - ord[c2];
            i++; j++;
        }
        if (i < n) return 1;
        if (j < m) return -1;
        return 0;
    }
    public boolean isAlienSorted(String[] words, String order) {
        for (int i = 0; i < 26; i++) ord[order.charAt(i) - 'a'] = i;
        int n = words.length;
        for (int i = 1; i < n; i++) {
            if (check(words[i - 1], words[i]) > 0) return false;
        }
        return true;
    }
}
复制代码
  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( C ) O(C)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.953 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour…

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

猜你喜欢

转载自juejin.im/post/7098511353698074660