leetcode 748. Shortest Completing Word (python)

该博客介绍了如何利用Python解决LeetCode上的Shortest Completing Word问题。通过将输入的车牌号转化为小写并移除数字和空格,然后使用Counter计算每个字符的频率,对单词列表按长度排序,遍历找到第一个包含所有字符的单词作为最短完成词。提供的解决方案具有良好的时间和空间效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

Given a string licensePlate and an array of strings words, find the shortest completing word in words.

A completing word is a word that contains all the letters in licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more than once in licensePlate, then it must appear in the word the same number of times or more.

For example, if licensePlate = “aBc 12c”, then it contains letters ‘a’, ‘b’ (ignoring case), and ‘c’ twice. Possible completing words are “abccdef”, “caaacab”, and “cbca”.

Return the shortest completing word in words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the first one that occurs in words.

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step","steps","stripe","stepple"]
Output: "steps"
Explanation: licensePlate contains letters 's', 'p', 's' (ignoring case), and 't'.
"step" contains 't' and 'p', but only contains 1 's'.
"steps" contains 't', 'p', and both 's' characters.
"stripe" is missing an 's'.
"stepple" is missing an 's'.
Since "steps" is the only word containing all the letters, that is the answer.	

Example 2:

Input: licensePlate = "1s3 456", words = ["looks","pest","stew","show"]
Output: "pest"
Explanation: licensePlate only contains the letter 's'. All the words contain 's', but among these "pest", "stew", and "show" are shortest. The answer is "pest" because it is the word that appears earliest of the 3.

Example 3:

Input: licensePlate = "Ah71752", words = ["suggest","letter","of","husband","easy","education","drug","prevent","writer","old"]
Output: "husband"

Example 4:

Input: licensePlate = "OgEu755", words = ["enough","these","play","wide","wonder","box","arrive","money","tax","thus"]
Output: "enough"

Example 5:

Input: licensePlate = "iMSlpe4", words = ["claim","consumer","student","camera","public","never","wonder","simple","thought","use"]
Output: "simple"

Note:

1 <= licensePlate.length <= 7
licensePlate contains digits, letters (uppercase or lowercase), or space ' '.
1 <= words.length <= 1000
1 <= words[i].length <= 15
words[i] consists of lower case English letters.

解析

根据题意,就是将 licensePlate 小写之后去掉数字和空格的 licensePlate ,然后从 words 中找出最短的单词,并且它包括了 licensePlate 中出现的所有的小写字母。思路就是这样简单,先将 licensePlate 字母小写化后用正则去掉数字和空格,然后对 words 按照单词长度生序排序,最后遍历 words 中的每个单词,找出包含了 licensePlate 中出现的所有小写字母的最短单词。

解答

class Solution(object):
    def shortestCompletingWord(self, licensePlate, words):
        """
        :type licensePlate: str
        :type words: List[str]
        :rtype: str
        """
        # lower
        licensePlate = licensePlate.lower()
        # remove digit
        p1 = re.compile(r"[0-9\s]")
        licensePlate = p1.sub("", licensePlate)
        # count every char freq
        counter = collections.Counter(licensePlate)
        # sort by length
        words.sort(key=lambda x:len(x))
        for word in words:
            for k, v in counter.items():
                if k not in word or word.count(k) < v:
                    break
            else:
                return word

运行结果

Runtime: 52 ms, faster than 87.34% of Python online submissions for Shortest Completing Word.
Memory Usage: 13.7 MB, less than 89.87% of Python online submissions for Shortest Completing Word.

原题链接:https://leetcode.com/problems/shortest-completing-word/

您的支持是我最大的动力

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大丫丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值