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.

使用Map将字典序映射为数字,进行比较

class Solution {
public boolean compare(Map<Character,Integer> ch,String string1,String string2){
int len1 = string1.length();
int len2 = string2.length();
int len = len1 < len2 ?len1:len2;
for(int i =0;i<len;i++){
if(ch.get(string1.charAt(i)) < ch.get(string2.charAt(i))) return true;
else if(ch.get(string1.charAt(i)) == ch.get(string2.charAt(i)))
continue;
else
return false;
}
if(len1<=len2) return true;
else return false;
}
public boolean isAlienSorted(String[] words, String order) {
Map<Character,Integer> ch = new HashMap<Character,Integer>();
for(int i =0;i<order.length();i++){
ch.put(order.charAt(i),i);
}
for(int i=1;i<words.length;i++){
if(!compare(ch,words[i-1],words[i])) return false;
}
return true;
}
}

953.Verifying an Alien Dictionary(Map)的更多相关文章

  1. 【Leetcode_easy】953. Verifying an Alien Dictionary

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

  2. LeetCode 953. Verifying an Alien Dictionary

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

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

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

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

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

  5. 【leetcode】953. Verifying an Alien Dictionary

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

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

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

  7. Verifying an Alien Dictionary

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

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

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

  9. LeetCode.953-验证外语字典顺序(Verifying an Alien Dictionary)

    这是悦乐书的第364次更新,第392篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第226题(顺位题号是953).在外语中,令人惊讶的是,他们也使用英文小写字母,但可能使 ...

随机推荐

  1. PMP知识点(一)——风险登记册

    一.Reference: [管理心得之四十八]<风险登记册>本身的风险 问题日志与风险登记册的区别与联系 PMBOK重要概念梳理之二十六 风险登记册 风险登记单-MBAlib 二.Atta ...

  2. json基础小结

    定义:json是一种前后端数据传送的格式规定json对象,json字符串 (区别 json字符串是有json格式的字符串)1.创建(两中json结构,一种是对象,一种是数组)json对象:var ao ...

  3. Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法

    https://www.jb51.net/article/133836.htm 搭建服务器之后只能看见test与infomation_schema两个库 https://www.cnblogs.com ...

  4. c#串口编程(转)

    在单片机项目开发中,上位机也是一个很重要的部分,主要用于数据显示(波形.温度等).用户控制(LED,继电器等),下位机(单片机)与 上位机之间要进行数据通信的两种方式都是基于串口的: USB转串口 — ...

  5. WPF实战之一 桌面消息框(右下角消息弹出框)

    此版本是根据别人的项目改造的,记录下笔记 原文:https://blog.csdn.net/catshitone/article/details/75089069 一.即时弹出 1.创建弹出框 新建一 ...

  6. ASP.NET Core 项目配置 ( Startup )(转载)

    原文:https://www.twle.cn/l/yufei/aspnetcore/dotnet-aspnet-startup.html 由于是个人网站,怕没了,特意复制保存,个人觉得讲的非常透彻 前 ...

  7. jenkins主要目录用途

    主目录 除了Jenkins的WAR包所在目录,Jenkins还有一个更重要的目录——Jenkins的所有重要数据都存放在这个独立的目录中,称为Jenkins主目录,它的默认位置是在当前用户根目录的隐藏 ...

  8. 【原创】Linux基础之vi

    vi配置文件 ~/.vimrcor/etc/vimrc 模式 命令模式(Command Mode) 1 上/下/左/右移动光标 i/k/j/l 2 跳到文件末尾 G 3 跳到文件开头 gg 4 向下搜 ...

  9. 关于 git 本地创建 SSH Key 遇到的一点问题(①file to save the key & ②the authenticity of host...)

    背景 由于想测试一下 SSH Key 创建的路径(.ssh 目录路径)对于不同位置 git 项目是否有效. 比如,.ssh 默认在 C:\[users]\[username] 目录下,而项目 proj ...

  10. 最优的路线(floyd最小环)

    问题描述 学校里面有N个景点.两个景点之间可能直接有道路相连,用Dist[I,J]表示它的长度:否则它们之间没有直接的道路相连.这里所说的道路是没有规定方向的,也就是说,如果从I到J有直接的道路,那么 ...