zoukankan      html  css  js  c++  java
  • 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 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).
    

    Note:

    1. 1 <= words.length <= 100
    2. 1 <= words[i].length <= 20
    3. order.length == 26
    4. All characters in words[i] and order are english lowercase letters.

    把order中每个字母及其次序用hashmap表示,因为order的长度是已知且固定的,可以用数组代替hashmap,用 对应的字符 - a 表示该字符

    然后遍历words,分别比较每一个字符串与前一个字符串

    compare details: 比较s, t每个字符的大小,用cmp表示,如果当前字符不等,返回cmp即可;如果不越界的情况下比完了且都相等,再比较两个字符串的长度,返回两者之差即可

    time: O(mn)  -- m: length of String[] words, n: length of words, space: O(1)

    class Solution {
        public boolean isAlienSorted(String[] words, String order) {
            int[] map = new int[26];        
            for(int i = 0; i < 26; i++) {
                map[order.charAt(i) - 'a'] = i;
            }
            
            for(int i = 1; i < words.length; i++) {
                if(compare(words[i - 1], words[i], map) > 0)
                    return false;
            }
            return true;
        }
        
        // returns 1 if s1 > s2, -1 if s1 < s2, 0 if s1 == s2
        private int compare(String s, String t, int[] map) {
            int m = s.length(), n = t.length();
            for (int i = 0; i < m && i < n; i++) {
                int cmp = map[s.charAt(i) - 'a'] - map[t.charAt(i) - 'a'];
                if (cmp != 0) 
                    return cmp;
            }
            return m - n;        
        }
    }
  • 相关阅读:
    关键两招就解决Wampserver 打开localhost显示IIS7图片问题
    死磕!Windows下Apache+PHP+phpmyadmin的配置
    宝塔linux面板运行jsp文件的配置工作
    Python关于self用法重点分析
    Atom窗口切换和放大或者缩小
    容易掉坑的地方The value for the useBean class attribute XXX is invalid
    JS 之如何在插入元素时插在原有元素的前面而不是末尾
    ul或者ol中添加li元素
    页面右下角广告
    getAttribute与setAttribute
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10143227.html
Copyright © 2011-2022 走看看