Leetcode-Easy 953. Verifying an Alien Dictionary

简介: Leetcode-Easy 953. Verifying an Alien Dictionary

题目描述


给定一组单词和字母顺序,然后判断单词之间是否按字典序顺序排序。


字典序的理解:

设想一本英语字典里的单词,哪个在前哪个在后?

显然的做法是先按照第一个字母、以 a、b、c……z 的顺序排列;如果第一个字母一样,那么比较第二个、第三个乃至后面的字母。如果比到最后两个单词不一样长(比如,sigh 和 sight),那么把短者排在前。


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).


思路


创建一个字典序,代表每个字母的大小。然后将单词的排序转换为数字字符串排序:

比如'00'<'01','22'<'23'等


代码实现


class Solution:
    def isAlienSorted(self, words: 'List[str]', order: 'str') -> 'bool':
        ld=dict()
        for i in range(25,-1,-1):
            if i<10:
                ld[order[25-i]]='0'+str(i)
            else:
                ld[order[25-i]]=str(i)
        # print(ld)        
        for i in range(len(words)-1):
            lw,rw='',''
            for l in words[i]:
                lw=lw+str(ld[l])
            for l in words[i+1]:
                rw=rw+str(ld[l])
            lw=lw+(20-len(lw))*'9'
            rw=rw+(20-len(rw))*'9'
            # print(lw,rw)
            if lw<rw:
                return False
        return True


其他解法


自己写的运行效率还可以36ms,超过99.7%。然后去讨论区看了下大佬写的,顿时给跪了,思路差不多,但是简洁多了:

class Solution:
    def isAlienSorted(self, words, order):
        m = {c: i for i, c in enumerate(order)}
        words = [[m[c] for c in w] for w in words]
        return all(w1 <= w2 for w1, w2 in zip(words, words[1:]))


相关文章
|
Java
[LeetCode] Alien Dictionary
Problem Description: There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you.
884 0
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2
|
5天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
38 7