leetcode——30. 串联所有单词的子串

class Solution:
    def findSubstring(self, s: str, words):
        if s==‘‘ or words==[]:return []
        n=len(words)
        m=len(words[0])
        r=[]
        i=0
        while i<len(s)-m*n+1:
            for j in words:
                p=s[i:i+m*n]
                t=list(p[m*i:m*(i+1)] for i in range(n))
                if t.count(j)!=words.count(j):
                    i+=1
                    break
            else:
                r.append(i)
                i+=1
        return r

执行用时 :908 ms, 在所有 python3 提交中击败了48.75%的用户

内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.30%的用户

这里面用到 t 的原因是:

不用 t 的时候,比如:s = "ababaab", words = ["ab","ba","ba"] 就会报错!

错误原因:因为计算时候我们会从字符串中间计算,也就是说会出现单词截断的问题。

原文地址:https://www.cnblogs.com/taoyuxin/p/11699441.html

时间: 2024-11-12 14:17:14

leetcode——30. 串联所有单词的子串的相关文章

[LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】

[030-Substring with Concatenation of All Words(串联所有单词的子串)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concaten

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concate

[leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一关于s的字典,用来记录每个word在s中出现的所有位置,注意可能会出现相同的word.然后递归枚举words的排列情况,一一校验是否符合条件(即连在一起).用到了递归+记忆化搜索+kmp+几个剪枝 一直最后几个测试用例上TLE,囧啊,甚至一度怀疑是不是还有更优的做法. 然后开始考虑剪枝: 记忆化搜索

Leetcode——30.与所有单词相关联的字串【##】

@author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序. 示例 1: 输入: s = "barfoothefoobarman&qu

[LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, giv

30. 与所有单词相关联的字串、java实现

题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序. 示例 1: 输入: s = "barfoothefoobarman", words = ["foo","bar"] 输出: [0,9] 解释: 从索引 0 和 9 开始的子串分别是 "barfo

LeetCode 0079. Word Search单词搜索【Python】

LeetCode 0079. Word Search单词搜索[Medium][Python][DFS] Problem LeetCode Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horiz

LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)

翻译 给定一个字符串S,一个单词的列表words,全是相同的长度. 找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引. 原文 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word