某种外星语也使用英文小写字母,但可能顺序 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;
}
};

Leetcode953. Verifying an Alien Dictionary验证外星语词典的更多相关文章

  1. LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)

    题目标签:HashMap 题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序. 利用hashmap 把order 存入 map, ...

  2. [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  3. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  4. Verifying an Alien Dictionary

    2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...

  5. 953.Verifying an Alien Dictionary(Map)

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  6. LeetCode 953 Verifying an Alien Dictionary 解题报告

    题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...

  7. LeetCode 953. Verifying an Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/verifying-an-alien-dictionary/ 题目: In an alien language, surpr ...

  8. 【leetcode】953. Verifying an Alien Dictionary

    题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...

  9. 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Ubuntu Apache vhost不执行php小记

    运行环境: Ubuntu : 16.04 PHP: 5.6.36 Apache: 2.4.18 出现/var/www/html 文件夹下的 php文件能够执行 vhost 配置文件的DocumentR ...

  2. Linux CentOS 6.7 挂载U盘

    1. 首先查看U盘是否成功安装fdisk -l 2. 在/mnt下创建U盘目录mkdir /mnt/usb 3. 挂载U盘mount -t vfat /dev/sdb1 /mnt/usb 4. 卸载U ...

  3. 阿里云在云栖大会发布RPA最新3.4版本,将与达摩院联合探索人工智能领域

    9月26日,在2019年杭州云栖大会上,阿里云发布了RPA最新V3.4版本,全新升级了增加诸如录屏审计.JAVA应用录制能力.达摩院OCR内置组件.语法检查与智能提示能力增强等功能. RPA全名称Ro ...

  4. 「题解」:X国的军队

    问题 A: X国的军队 时间限制: 1 Sec  内存限制: 256 MB 题面 题面谢绝公开. 题解 简单贪心. 按照存活的士兵数量(即参加战斗的士兵数量减去阵亡的士兵数量)排序. 若存活士兵数量相 ...

  5. mysql中geometry类型的简单使用

    mysql中geometry类型的简单使用 编写本文的目的: 让和两天前的我一样的初学者,能够更快的使用geometry类型存储空间点数据    也是为了自己加深印象,更熟练的使用geometry类型 ...

  6. 16.ajax_case05

    # 抓取36氪快讯 # https://36kr.com/newsflashes import requests import json header = { 'Accept': 'text/html ...

  7. 《Practices of an Agile Developer:Woring in the Real World》读书笔记 PB16110698(~3.22)第三周

    <Practices of an Agile Developer:Woring in the Real World>读书笔记  本周我阅读了<高效程序员的45个习惯:敏捷开发修炼之道 ...

  8. Qt : 隐式数据共享(copy on write)

    copy on write 意思当内容有变动的时候,才对容器中的数据结构进行复制.否则仅作共享. QT许多类中使用了隐式数据共享技术,来最大化资源利用率和最小化拷贝时的资源消耗. 在数据传递时,其实只 ...

  9. error in ./src/pages/login.vue?vue&type=style&index=0&lang=less&

    vue-cli3创建less工程,npm run serve 无法运行 bug解决方法: rm -rf node-modules 修改package.json为 "less": & ...

  10. 前端基础之BOM与DOM操作

    目录 BOM操作 navigator对象 screen对象 history对象 localtion对象 弹出框 计时 setTimeout() clearTimeout() setInterval() ...