• Leetcode 953. 验证外星语词典(感觉还挺难的)


    在这里插入图片描述
    某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

    给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。

    示例 1:

    输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
    输出:true
    解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。
    

    示例 2:

    输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
    输出:false
    解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。
    

    示例 3:

    输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
    输出:false
    解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。
    

    提示:

    • 1 <= words.length <= 100
    • 1 <= words[i].length <= 20
    • order.length == 26
    • 在 words[i] 和 order 中的所有字符都是英文小写字母。

    主要思路:
    1.先构造字典
    2.依次遍历每个单词字符是否按照字典顺序

    Code:

    class Solution {
    public:
        //判断vector是否升序
        bool ordered(vector<int>&a)
        {
            for(int i=1;i<a.size();i++)
            {
                if(a[i]<a[i-1])
                    return false;
            }
            return true;
        }
        bool isAlienSorted(vector<string>& words, string order) {
            bool ret=true;
            map<char,int>dict;
            for(int i=0;i<order.size();i++)
            {
                dict[order[i]]=i;
            }
            dict['A']=-1;//-这里相当于是'∅'
            int maxlen=0;
            for(int i=0;i<words.size();i++)
            {
                maxlen=max(maxlen,(int)words[i].length());
            }
    
            vector<pair<char,int>>mymap;
    
            int cnt=0;
            int loop=words.size();
    
            for(int i=0;i<maxlen;i++)
            {
                for(int j=0;j<loop;j++)
                {
    
                    string t=words[j];
                    if(cnt<=(t.length()-1))
                    {
                        mymap.push_back(pair<char,int>(t[cnt],1));
                    }
                    else
                    {
                        mymap.push_back(pair<char,int>('A',1));
                    }
                }
    
    
                vector<int>vec;
                vector<pair<char,int>>::iterator it;
                for(it=mymap.begin();it!=mymap.end();++it)
                {
                    int pos=-1;
                    pos=dict[it->first];
                    vec.push_back(pos);
    
                }
                if(!ordered(vec))
                    return false;
    
                else
                {
    
                    int size=vec.size();
                    sort(vec.begin(),vec.end());
                    vec.erase(unique(vec.begin(),vec.end()),vec.end());
                    if(vec.size()==size)
                        return true;
    
                    for(int i=1;i<loop;i++)
                    {
                        if(words[i][cnt]!=words[i-1][cnt])
                        {
                            loop=i;
                            break;
                        }
    
                    }
                    mymap.clear();
    
                }
                cnt++;
            }
            return ret;
    
        }
    };
    
    
  • 相关阅读:
    Winform 让跨线程访问变得更简单
    Winform 单实例运行
    webservice 测试窗体只能用于来自本地计算机的请求
    使用srvany.exe把程序安装成windows服务
    学习ExtJS4 常用控件
    [leetcode]Unique Binary Search Trees
    [leetcode]Subsets II
    [leetcode]Subsets
    [leetcode]Valid Number
    [leetcode]Permutation Sequence
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/16294656.html
Copyright © 2020-2023  润新知