zoukankan      html  css  js  c++  java
  • LeetCode 953. Verifying an Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/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 lexicographicaly 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.

    题解:

    Given the order in string order, covert it into char with its index relationship.

    For current and previous string, check if previous string is bigger than current string. If yes, return false.

    In order to check if previous string is bigger than current string, get the corresponding char, when they are not equal, check if previous index is bigger than current one, if yes, return false.

    If they keep equal until the end, then check if previous string length > current string length.

    Time Complexity: O(nm + k). n = words.length(). m is average length. k = order.length(). k <= 26.

    Space: O(1).

    AC Java: 

     1 class Solution {
     2     public boolean isAlienSorted(String[] words, String order) {
     3         if(words == null || words.length == 0 || order == null || order.length() == 0){
     4             return true;
     5         }
     6         
     7         int [] map = new int[26];
     8         for(int i = 0; i<order.length(); i++){
     9             map[order.charAt(i)-'a'] = i;
    10         }
    11         
    12         for(int i = 1; i<words.length; i++){
    13             if(isBigger(words[i-1], words[i], map)){
    14                 return false;
    15             }
    16         }
    17         
    18         return true;
    19     }
    20     
    21     private boolean isBigger(String s1, String s2, int [] map){
    22         int j = 0;
    23         while(j < s1.length() && j < s2.length()){
    24             if(s1.charAt(j) != s2.charAt(j)){
    25                 return map[s1.charAt(j) - 'a'] > map[s2.charAt(j) - 'a'];
    26             }
    27             
    28             j++;
    29         }
    30         
    31         return s1.length() > s2.length();
    32     }
    33 }
  • 相关阅读:
    vue----计算与监听属性
    vue---条件与循环语句
    vue--模板语法
    git版本控制系统--介绍
    HttpWebRequest使用总结
    计算机专业的书普遍都这么贵,你们都是怎么获取资源的?
    130 个相见恨晚的超实用网站,一次性分享出来
    实现客户端与服务端的HTTP通讯
    默认网关(地址)
    .NET Reactor使用教程(加密源代码示例)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12008882.html
Copyright © 2011-2022 走看看