LeetCode题解(1178):猜字谜(Python)
发布日期:2021-06-29 20:09:46 浏览次数:2 分类:技术文章

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

题目:(困难)

标签:哈希表、位运算

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( W × L × l o g L + P × 2 7 × 7 × l o g 7 ) O(W×L×logL+P×2^7×7×log7) O(W×L×logL+P×27×7×log7) : 其中L为谜底长度 O ( W × L + 2 7 × 7 ) O(W×L+2^7×7) O(W×L+27×7) 916ms (35.29%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:        def all_chance(chars):            lst = [chars[0]]            chars = list(sorted(set(chars[1:])))            size = len(chars)            res = []            def recursion(i):                if i == size:                    res.append("".join(sorted(lst)))                else:                    lst.append(chars[i])                    recursion(i + 1)                    lst.pop()                    recursion(i + 1)            recursion(0)            return res        count = collections.Counter()        for word in words:            count["".join(sorted(set(word)))] += 1        ans = []        for puzzle in puzzles:            now = 0            for chance in all_chance(puzzle):                now += count[chance]            ans.append(now)        return ans

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

上一篇:LeetCode题解(1198):找出所有行中最小公共元素(Python)
下一篇:LeetCode题解(1166):设计文件系统(Python)

发表评论

最新留言

很好
[***.229.124.182]2024年04月17日 18时31分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章