题目如下:

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.

Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.)
According to lexicographical rules "apple" > "app", because 'l' > '∅',
where '∅' is defined as the blank character which is less than any other character (More info).

Note:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters in words[i] and order are english lowercase letters.

解题思路:题目很长,但是很简单,说的简单点就是自定义了一个字典序,用这个自定义的字典序判断数组中字符串是否是升序的。

代码如下:

class Solution(object):
def isAlienSorted(self, words, order):
"""
:type words: List[str]
:type order: str
:rtype: bool
"""
dic = {}
for i,v in enumerate(order):
dic[v] = i def cmpf(s1,s2,dic):
for i in range(min(len(s1),len(s2))):
if dic[s1[i]] > dic[s2[i]]:
return 1
elif dic[s1[i]] < dic[s2[i]]:
return -1
if len(s1) > len(s2):
return 1
else:
return -1
for i in range(len(words)-1):
if cmpf(words[i],words[i+1],dic) == 1:
return False
return True

【leetcode】953. Verifying an Alien Dictionary的更多相关文章

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

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

  2. 【Leetcode_easy】953. Verifying an Alien Dictionary

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

  3. LeetCode 953. Verifying an Alien Dictionary

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

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

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

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

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

  6. 953.Verifying an Alien Dictionary(Map)

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

  7. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  8. Verifying an Alien Dictionary

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

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. SQL 左连接

    SQL左链接LEFT JOIN关键字返回左表(表1)中的所有行,即使在右表(表2)中没有匹配.如果在正确的表中没有匹配,结果是NULL. SQL LEFT JOIN 语法 SELECT _column ...

  2. AcWing 227. 小部件厂 (高斯消元)打卡

    题目:https://www.acwing.com/problem/content/description/229/ 题意:有很多个零件,每个零件的生产时间都在3-9天之间,现在只知道每个工人的生产部 ...

  3. CSS 的基础语法

    1.基础语法规则 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 每条声明由一个属性和一个值组成.每个属性有一个值.属性和值被冒号分开. selector {property: val ...

  4. Spring Boot 报错记录

    Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...

  5. ORACLE切非归档模式:

    C:\Documents and Settings\Administrator>sqlplus /nologSQL> conn / as sysdbaConnected to an idl ...

  6. C++构造函数异常(二)

    继续上一篇文章提到的构造异常话题,下面继续谈另外两个场景,即多继承构造异常,以及智能指针构造异常 第3:对多继承当中,某个基类构造异常,而其他基类已构造成功,则构造成功的基类不会析构,由编译器负责回收 ...

  7. git操作的日常用法

    参考博客:  https://blog.csdn.net/afei__/article/details/51567155# 最近一段时间总结一些git在个人日常开发当中用到的方法, 并记录下来, 同时 ...

  8. Django框架(十五)—— forms组件、局部钩子、全局钩子

    目录 forms组件.局部钩子.全局钩子 一.什么是forms组件 二.forms组件的使用 1.使用语法 2.组件的参数 3.注意点 三.渲染模板 四.渲染错误信息 五.局部钩子 1.什么是局部钩子 ...

  9. PHP数组循环遍历的几种方式

    PHP数组循环遍历 1.for循环 <?php //语法 for (init counter; test counter; increment counter) { code to be exe ...

  10. Module not found: Error: Can't resolve '@babel/runtime/helpers/classCallCheck' and Module not found: Error: Can't resolve '@babel/runtime/helpers/defineProperty'

    These two mistakes are really just one mistake, This is because the following file @babel/runtime ca ...