leetcode - 953. Verifying an Alien Dictionary

Description

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

Constraints:

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

Solution

Map the letters to the old order, and compare each pair.

Time complexity: o ( n ∗ m ) o(n*m) o(nm), where n is the length of words, m is the length of each word in words
Space complexity: o ( 1 ) o(1) o(1)

Code

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        def a_smaller_than_b(a: str, b: str) -> bool:
            """
            Returns:
                True: if a <= b
                False: if a > b
            """
            p1, p2 = 0, 0
            while p1 < len(a) and p2 < len(b):
                if cmp_order[a[p1]] > cmp_order[b[p2]]:
                    return False
                elif cmp_order[a[p1]] < cmp_order[b[p2]]:
                    return True
                p1 += 1
                p2 += 1
            return p1 == len(a)
        cmp_order = {}
        for i, c in enumerate(order):
            cmp_order[c] = i
        for i in range(1, len(words)):
            if not a_smaller_than_b(words[i - 1], words[i]):
                return False
        return True

相关推荐

  1. leetcode - 953. Verifying an Alien Dictionary

    2024-03-12 23:04:08       42 阅读
  2. LeetCode //C - 933. Number of Recent Calls

    2024-03-12 23:04:08       56 阅读
  3. leetcode93. 复原 IP 地址

    2024-03-12 23:04:08       48 阅读
  4. leetcode 93. 复原 IP 地址

    2024-03-12 23:04:08       70 阅读
  5. LeetCode 93. 复原 IP 地址

    2024-03-12 23:04:08       56 阅读
  6. LeetCode 93. 复原 IP 地址

    2024-03-12 23:04:08       41 阅读
  7. leetcode93.复原IP地址

    2024-03-12 23:04:08       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-12 23:04:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-12 23:04:08       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-12 23:04:08       82 阅读
  4. Python语言-面向对象

    2024-03-12 23:04:08       91 阅读

热门阅读

  1. 使用hashmap优化时间复杂度,leetcode1577

    2024-03-12 23:04:08       46 阅读
  2. 计算机等级考试:信息安全技术 知识点六

    2024-03-12 23:04:08       43 阅读
  3. 【Go】探索Go语言中的关于defer的应用

    2024-03-12 23:04:08       45 阅读
  4. VSCode无法用ctrl+鼠标滚轮调整字体大小了

    2024-03-12 23:04:08       37 阅读
  5. python中的幂运算

    2024-03-12 23:04:08       40 阅读
  6. Python sort从大到小排序面试题

    2024-03-12 23:04:08       36 阅读
  7. Spring Data的Repositories----自定义存储库实现

    2024-03-12 23:04:08       38 阅读
  8. SpringBoot-WEB相关

    2024-03-12 23:04:08       37 阅读
  9. 如何实现单片机与手机的远距离通信

    2024-03-12 23:04:08       35 阅读