zoukankan      html  css  js  c++  java
  • [LeetCode] 953. 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.

    验证外星语字典。题目即是题意,给了一些单词和一个定好的字典序,请你验证单词是否是有序的。这道题虽然看上去跟269很像但是会简单很多,思路是把order里面的字母之间的顺序转化为hashmap<letter, order>,然后再对words里面的单词进行两两比较。比较的方式是按字母逐个比较,如果同样位置上的两个字母不一样,则判断是否第一个单词的字母的字典序更小,若不是就return false;如果判断完了并没有找到false的情形,则判断是否第一个单词更短,因为除了满足字典序之外,也需要满足更短的单词在前。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean isAlienSorted(String[] words, String order) {
     3         HashMap<Character, Integer> map = new HashMap<>();
     4         for (int i = 0; i < order.length(); i++) {
     5             char ch = order.charAt(i);
     6             map.put(ch, i);
     7         }
     8 
     9         for (int i = 0; i < words.length - 1; i++) {
    10             if (!inorder(words[i], words[i + 1], map)) {
    11                 return false;
    12             }
    13         }
    14         return true;
    15     }
    16 
    17     private boolean inorder(String w1, String w2, HashMap<Character, Integer> map) {
    18         for (int i = 0; i < w1.length() && i < w2.length(); i++) {
    19             char ch1 = w1.charAt(i);
    20             char ch2 = w2.charAt(i);
    21             int index1 = map.get(ch1);
    22             int index2 = map.get(ch2);
    23             if (index1 < index2) {
    24                 return true;
    25             } else if (index1 > index2) {
    26                 return false;
    27             }
    28         }
    29         return w1.length() <= w2.length();
    30     }
    31 }

    相关题目

    207. Course Schedule

    210. Course Schedule II

    269. Alien Dictionary

    310. Minimum Height Trees

    953. Verifying an Alien Dictionary

    LeetCode 题目总结

  • 相关阅读:
    基于windows的mysql读写分离和amoeba配置
    windows环境下mysql主从配置
    Mysql主从报错锦集
    Nginx流控
    windows_windows下的rsync
    Kafka+ZooKeeper高可用集群部署
    通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布
    Redis性能指标监控
    MongoDB用户权限管理
    ZooKeeper原理详解及常用操作
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12894080.html
Copyright © 2011-2022 走看看