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 }
  • 相关阅读:
    类的特殊获取方式——《Thinking in Java》随笔008
    方法排序规范——《Thinking in Java》随笔007
    权限修饰符(访问指示符)——《Thinking in Java》随笔006
    史上最强,万能类型:Object——《Thinking in Java》随笔005
    七天C#小结
    编译原理10 消除左递归
    编译原理9 DFA最小化,语法分析初步
    编译原理8 非确定的自动机NFA确定化为DFA
    编译原理7 正规式、正规文法与自动机
    编译原理6 正规文法与正规式
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12008882.html
Copyright © 2011-2022 走看看