剑指offer034-外星语言是否排序

外星语言是否排序

Posted by 高明 on 2020-01-01

剑指offer034-外星语言是否排序

题目

某种外星语也使用英文小写字母,但可能顺序 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 中的所有字符都是英文小写字母。

 

注意:本题与主站 953 题相同: https://leetcode-cn.com/problems/verifying-an-alien-dictionary/

Related Topics
  • 数组
  • 哈希表
  • 字符串

  • 👍 11
  • 👎 0
  • 思路

    这题目没什么特别的思路,就是一个一个比较

    首先用map定义字母的顺序

    1
    2
    3
    4
    Map<Character, Integer> orderMap = new HashMap<>();
    for (int i = 0; i < order.length(); i++) {
    orderMap.put(order.charAt(i), i);
    }

    因为判断数组单词是否字母序,且如果A<B,B<C,那么A<C,所以只需要判断相邻的单词是否满足A<B即可

    1
    2
    3
    4
    5
    for (int i = 0; i < words.length - 1; i++) {
    String word1 = words[i];
    String word2 = words[i + 1];
    // 比较 word1 和 word2 即可
    }

    单词之间的相互比较,如果比如

    a b d
    a c e

    其实如果对应位置上的单词相等,则判断下一个

    如果对应位置上的单词不相等,能判断两个单词的大小关系,但是对于整个数组,二者的大小关系不能决定整个数组的大小关系

    存在一种情况,如果长度不等,且前缀都是一致的

    a b c
    a b

    很显然ab < abc,但是由于可能忽略这种情况,导致比较到最后都是相等的,直接返回AB相等

    这种情况可以设置没有元素的地方为-1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // word1 和 word2 的最大长度
    int index = Math.max(word1.length(), word2.length());
    for (int j = 0; j < index; j++) {
    // 单词1的字母序,为空的时候为-1
    int v1 = j < word1.length() ? orderMap.get(word1.charAt(j)) : -1;
    // 单词2的字母序,为空的时候为-1
    int v2 = j < word2.length() ? orderMap.get(word2.charAt(j)) : -1;
    // 如果 单词1 > 单词2,直接返回False
    if (v1 > v2) {
    return false;
    } else if (v1 < v2) {
    // 反之可以继续比较
    break;
    }
    }

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    class Solution {
    public boolean isAlienSorted(String[] words, String order) {
    Map<Character, Integer> orderMap = new HashMap<>();
    for (int i = 0; i < order.length(); i++) {
    orderMap.put(order.charAt(i), i);
    }

    for (int i = 0; i < words.length - 1; i++) {
    String word1 = words[i];
    String word2 = words[i + 1];
    int index = Math.max(word1.length(), word2.length());
    for (int j = 0; j < index; j++) {

    int v1 = j < word1.length() ? orderMap.get(word1.charAt(j)) : -1;
    int v2 = j < word2.length() ? orderMap.get(word2.charAt(j)) : -1;

    if (v1 > v2) {
    return false;
    } else if (v1 < v2) {
    break;
    }
    }
    }
    return true;
    }
    }