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.

    解题思路:

    从题干中可以提取出以下重点:

      1. 字典中全部为小写字母,lowercase, 这代表不需要我们自己检查。

      2. 给出的字符串为新的顺序

      3. 词本中可能存在100个一内的单词,每个单词长度小于20

      

    对词本中出现的单词,需要和它前面(后面)的单词进行比较,这里我选择的是是前面:

      1. 第一个不同的字母需要按照规定顺序排序

      2. 若存在单词w1是w2的前半部分:abc, abcd,短的应该在前面。

    因为词本出现的词数较多,所以我们用hashmap来存储每个字母对应的下标,通过对比下标得到是否遵循顺序

    这里需要考虑到的edge case 就是上述第二条

    代码:

    class Solution {
    public:
        bool isAlienSorted(vector<string>& words, string order) {
            if(words.size() <= 1) 
                return true;
            
            unordered_map<char,int> charIdx;
            for(int i = 0; i < order.size(); i++){
                charIdx[order[i]] = i;
            }
            //check words order
            string prev = words[0];
            for(int i = 1; i < words.size(); i++){
                string cur = words[i];
                int j = 0;
                int minSize = min(prev.size(), cur.size());
                while(j < minSize && prev[j] == cur[j]){
                    j++;
                }
                if(j == minSize){
                    if(minSize < prev.size()) return false;
                    continue;
                }
                if(charIdx[prev[j]] > charIdx[cur[j]]) return false;
                prev = cur;
            }
            return true;
        }
    };
  • 相关阅读:
    zepto引用touch模块后,click失效
    cocos2dx中setContentSize与setDimensions在设置label显示区域上的区别
    convertToNodeSpace 与 convertToWorldSpace 的使用
    quick(3.2) UIListView扩展
    quick-cocos2d-x 游戏开发——StateMachine 状态机
    Lua学习笔记之字符串及模式匹配
    cocos2dx-3.x事件分发机制
    cocos2dx-Lua与Object的通讯机制
    cocos2dx-Lua与Java通讯机制
    Quick-cocos2dx容器层的使用
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/11589375.html
Copyright © 2011-2022 走看看