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. [C++]UVaLive7324 ASCII Addtion

    Description Nowadays, there are smartphone applications that instantly translate text and even solve ...

  2. python3 练手实例8 批量命名图片

    #coding:utf-8 import os import tkinter as tk from tkinter import filedialog root = tk.Tk() root.with ...

  3. IDEA中 GIT与SVN版本控制插件的切换

    https://www.cnblogs.com/yccmelody/p/7794220.html

  4. 关于this绑定的四种方式

    一.前言 我们每天都在书写着有关于this的javascript代码,似懂非懂地在用着.前阵子在看了<你不知道的JavaScript上卷>之后,也算是被扫盲了一边关于this绑定的四种方式 ...

  5. [转】Python--遍历列表时删除元素的正确做法

    转自:https://blog.csdn.net/cckavin/article/details/83618306   一.问题描述 这是在工作中遇到的一段代码,原理大概和下面类似(判断某一个元素是否 ...

  6. 【medium】990. Satisfiability of Equality Equations 并查集

    Given an array equations of strings that represent relationships between variables, each string equa ...

  7. [insight] debug

    python: 1. print理解流程 print('xy1') print('xy2') 可以更好地跟踪函数的执行流程,分析代码 2. 用python库 import pdb; pdb.set_t ...

  8. [JLOI2014]松鼠的新家-树链剖分

    最开始的时候我在写线段树部分的时候还打了一个build,后来一想,打个球球大作战的build啊!!!有个锤子的用啊!!! #include<bits/stdc++.h> using nam ...

  9. Python教学课程分享

    一.Python基本语法 1.1整数,实数,复数 前言: Python编程环境及方法 在python底层环境中唤醒python对话,直接在python中输入指令对系统进行命令编程 随意打开一个记事本或 ...

  10. Java - day001 - 8种基本数据类型

    一字节8电位 Java byte 最左边一位表示正负,0是正,1是负. (option / alt)  + / 代码提示 Refactor 重构 Rename 重命名  (windows 重命名是选中 ...