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 }
  • 相关阅读:
    纯MATLAB版本 SIFT代码
    hadoop2.4集群的搭建
    hadoop2.4的伪集群的搭建
    linux 下的ssh免密登陆设置
    linux 下的常用操作命令
    linux中的IP地址的修改
    使用idea创建maven多模块项目
    Hudson的使用
    创建线程的几种方法
    idea添加内存
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12008882.html
Copyright © 2011-2022 走看看