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

牛牛的串联子串游戏

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

滑动窗口

全部评论

相关推荐

07-17 12:07
门头沟学院 Java
勇敢牛牛不怕困难
投递OPPO等公司7个岗位
点赞 评论 收藏
分享
写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
07-18 15:02
门头沟学院 Java
刚打开网申页面就不想填了,还是不要为难自己了
poppinzhan...:多益老行业毒瘤了,碰到徐波这种恶心的烂人,去了也是受罪。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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