题目如下:

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:

  1. Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
  2. Output: true
  3. Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:

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

Example 3:

  1. Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
  2. Output: false
  3. 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.

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

代码如下:

  1. class Solution(object):
  2. def isAlienSorted(self, words, order):
  3. """
  4. :type words: List[str]
  5. :type order: str
  6. :rtype: bool
  7. """
  8. dic = {}
  9. for i,v in enumerate(order):
  10. dic[v] = i
  11.  
  12. def cmpf(s1,s2,dic):
  13. for i in range(min(len(s1),len(s2))):
  14. if dic[s1[i]] > dic[s2[i]]:
  15. return 1
  16. elif dic[s1[i]] < dic[s2[i]]:
  17. return -1
  18. if len(s1) > len(s2):
  19. return 1
  20. else:
  21. return -1
  22. for i in range(len(words)-1):
  23. if cmpf(words[i],words[i+1],dic) == 1:
  24. return False
  25. 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. jQuery HTML- 添加元素

    添加内容 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  2. JavaSE---多线程---概述

    1.概述 1.1 进程: 系统进行资源分配.调度的一个独立单元: 进程的特征: 1.1.1 独立性: 系统中独立存在的实体,拥有自己独立的资源: 每个进程都拥有自己私有的地址空间,在没有经过进程本身允 ...

  3. 记录 SpringBoot 踩坑经历

    1.spring-boot-starter-web 作用 <dependency> <groupId>org.springframework.boot</groupId& ...

  4. 模板方法模式TemplateMethod

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11407071.html 1. 定义定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子 ...

  5. boost smart pointer

    1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and ...

  6. spring的 onApplicationEvent方法被执行两次问题

    原文地址:http://www.cnblogs.com/a757956132/p/5039438.html 在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. ...

  7. 架构师技能树skill-map

    # 架构师技能树 ## 系统架构能力 ### 基本理论- 扩展性设计- 可用性设计- 可靠性设计- 一致性设计- 负载均衡设计- 过载保护设计 ### 协议设计- 二进制协议- 文本协议 ### 接入 ...

  8. 88、展示Tensorflow计算图上每个节点的基本信息以及运行时消耗的时间和空间

    ''' Created on May 24, 2017 @author: p0079482 ''' #使用程序输出日志 import tensorflow as tf with tf.Session( ...

  9. Linux / Unix Command: rz

    yum install lrzsz Most communications programs invoke rz and sz automatically. You can also connect ...

  10. RTTI RAII

    RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的 ...