LeetCode题解(0748):寻找符合要求的最短单词(Python)
发布日期:2021-06-29 19:54:13 浏览次数:2 分类:技术文章

本文共 649 字,大约阅读时间需要 2 分钟。

题目:[原题链接](https://leetcode-cn.com/problems/shortest-completing-word/(简单)

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1) 76ms (95.30%)
Ans 2 (Python)
Ans 3 (Python)

LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。

解法一:

def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:    patterns = list(filter(str.isalpha, licensePlate.lower()))    ans = " " * 15    for word in words:        alpha = list(word)        for p in patterns:            if p not in alpha:                break            alpha.remove(p)        else:            if len(word) < len(ans):                ans = word    return ans

转载地址:https://dataartist.blog.csdn.net/article/details/107063620 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0762):二进制表示中质数个计算置位(Python)
下一篇:LeetCode题解(0747):寻找数组中至少是其他数字两倍的最大数(Python)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月11日 18时05分23秒