LeetCode题解(0809):判断字符串是否由单词扩张形成(Python)
发布日期:2021-06-29 19:58:11 浏览次数:2 分类:技术文章

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

题目:(中等)

标签:字符串

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

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

解法一:

class Solution:    def expressiveWords(self, S: str, words: List[str]) -> int:        def change(s):            exp = []            for ch in s:                if not exp or ch != exp[-1][0]:                    exp.append([ch, 1])                else:                    exp[-1][1] += 1            return exp        S = change(S)        N = len(S)        ans = 0        for word in words:            word = change(word)            if len(word) == N:                for i in range(N):                    if word[i][0] != S[i][0] or word[i][1] > S[i][1] or (word[i][1] == 1 and S[i][1] == 2):                        break                else:                    ans += 1        return ans

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

上一篇:LeetCode题解(0816):模糊坐标(Python)
下一篇:LeetCode题解(0791):依据字符在另一个字符串中的出现顺序排序字符串(Python)

发表评论

最新留言

不错!
[***.144.177.141]2024年05月01日 13时12分51秒