栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

30. 串联所有单词的子串

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

30. 串联所有单词的子串

'''
Author: 365JHWZGo
Description: 30. 串联所有单词的子串
Date: 2021-10-06 10:49:14
FilePath: Pythontestdemo3.py
LastEditTime: 2021-10-06 11:18:45
LastEditors: 365JHWZGo
'''
#思路1:找到words中不同的组合方式,然后判断其每一次在s中出现的位置
#思路2:题中给了条件,每个单词长度相同,可以一次判断是否在其中,然后删除

class Solution:
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        #wordLen 单词长度
        #wordsLen 单词数组长度
        #result 结果
        result = []
        wordsTemp = words + []
        if words:
            wordLen = len(words[0])
        wordsLen = len(words)
        i = 0
        #特殊情况全是一个字母组成的s
        if len(set(s))==1 and len(s) == wordsLen:
            return [0]
        if len(set(s))==1 and len(set(words))==1 and wordLen==1:
            return [i for i in range(len(s)-wordLen*wordsLen+1)]
        while i < len(s):
            for j in range(wordsLen):
                part = s[i + j * wordLen:i + j * wordLen + wordLen]
                if part in words:
                    words.remove(part)
                else:                    
                    break
            if len(words) == 0:
                result.append(i)
            #方框移动                
            i = i + 1
            #恢复原来的words
            words = wordsTemp + []
        return result


if __name__ == '__main__':
    p = Solution()
    res = p.findSubstring("a", ["a"])
    print(res)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号