zoukankan      html  css  js  c++  java
  • Leetcode953. Verifying an Alien Dictionary验证外星语词典

    某种外星语也使用英文小写字母,但可能顺序 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. 1 <= words.length <= 100
    2. 1 <= words[i].length <= 20
    3. order.length == 26
    4. 在 words[i] 和 order 中的所有字符都是英文小写字母。

     map<char, int> dict;
    
     bool cmp(string x, string y)
     {
    	 int i =0,  j = 0;
    	 int len1 = x.size();
    	 int len2 = y.size();
    	 while (i < len1 && j < len2)
    	 {
    		 if (x[i] == y[j])
    		 {
    			 i++;
    			 j++;
    		 }
    		 else if (dict[x[i]] > dict[y[i]])
    		 {
    			 return false;
    		 }
    		 else
    		 {
    			 return true;
    		 }
    	 }
    	 return i == j ? true : i > j ? false : true;
     }
    
     class Solution {
     public:
    	 bool isAlienSorted(vector<string>& words, string order) 
    	 {
    		 for (int i = 0; i < 26; i++)
    		 {
    			 dict[order[i]] = i;
    		 }
    		 vector<string> newAry = words;
    		 sort(newAry.begin(), newAry.end(), cmp);
    		 for (int i = 0; i < words.size(); i++)
    		 {
    			 if (newAry[i] != words[i])
    				 return false;
    		 }
    		 return true;
    	 }
     };
  • 相关阅读:
    【转】内部Handler类引起内存泄露
    检测是否存在相机硬件代码
    asp.net 过滤器
    iis 中经典和集成模式对应webconfig节点
    事务
    C# Excel操作
    一步一步部署SSIS包图解教程
    js和.net操作Cookie遇到的问题
    File,FileInfo,Directory,DirectoryInfo
    C#文件Copy
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433781.html
Copyright © 2011-2022 走看看