题解 | #牛牛的串联子串游戏#

牛牛的串联子串游戏

https://www.nowcoder.com/practice/c1984371372b43f3b10bf6d0231520bb

class Solution:
    def findSubstring(self , s: str, words: List[str]) -> List[int]:
        # write code here
        ans, wordLen = [], len(words[0])

        cnt = dict()
        for i, word in enumerate(words):
            cnt[word] = i

        strs = []
        for index in range(0, len(s), wordLen):
            if s[index:index+wordLen] in words:
                strs.append(cnt[s[index:index+wordLen]])
            else:
                strs.append(-1)

        seen = []
        for i, ss in enumerate(strs):

            if ss == -1:
                if len(seen) == len(words):
                    ans.append(i - len(words))
                seen.clear()
                continue

            if ss not in seen:
                seen.append(ss)
            else:
                if len(seen) == len(words):
                    ans.append(i - len(words))
                while seen and ss in seen:
                    seen.pop(0)


        if seen and len(seen) == len(words):
            ans.append(len(words) - len(seen))

        for i, a in enumerate(ans):
            ans[i] = a * wordLen

        return ans

滑动窗口

全部评论

相关推荐

2025-12-29 20:37
已编辑
清华大学附属小学 Java
哇哇的菜鸡oc:1.为什么要写这么多内容呀 2.什么叫做简历 3.什么样的内容可以写到简历上 4.项目可以包装,但是要有理有据呀,不能乱包装呀,比如 跨境能达到日均120万订单的在国内都是能叫的上名字的,而且这些工作也基本上不太会交给一个实习生去做 建议友友可以去网上或者找同学的简历看看,他们的简历是怎么写的,去找找上面的那四个问题的答案吧,然后要记住的是Java是服务于业务的,而不是服务于微服务或者技术的
最后再改一次简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务