[LeetCode]题解(python):030-Substring with Concatenation of All Words

题目来源:

  https://leetcode.com/problems/substring-with-concatenation-of-all-words/



题意分析:

  输入一个字符串s和一连串的长度相同的字符串数组words,找出仅由所有的words组成的s的子字符串起始位置。



题目思路:

  由于给定的words的长度是相同的,题目难度就降低了很多。题目难度就在于判断一个字符串是否仅由words构成。这里,我们可以构造一个字典,key对应的是words出现的次数。将字符串截成n个words长度的字符串,如果这些字符串出现在字典里面,字典对应的次数-1.如果所有字典为0则满足。



代码(python):

 1 class Solution(object):
 2     def match(self,s,dict,size):
 3         i = 0
 4         while i <= len(s)- size:
 5             tmp = s[i:i + size]
 6             if tmp in dict and dict[tmp] != 0:
 7                 dict[tmp] -= 1
 8             else:
 9                 return False
10             i += size
11         return True
12     def findSubstring(self, s, words):
13         """
14         :type s: str
15         :type words: List[str]
16         :rtype: List[int]
17         """
18         sizew = len(words)
19         if sizew == 0:
20             return []
21         d = {}
22         ans = []
23         for i in words:
24             if i in d:
25                 d[i] += 1
26             else:
27                 d[i] = 1
28         j = 0
29         ss = len(s); sw = len(words[0])
30         while j <= ss - sizew * sw:
31             tmpd = d.copy()
32             if self.match(s[j:j + sizew * sw],tmpd,sw):
33                 ans.append(j)
34             j += 1
35         return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4898993.html

时间: 2024-08-09 02:02:29

[LeetCode]题解(python):030-Substring with Concatenation of All Words的相关文章

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 030. Substring with Concatenation of All Words (Hard) 链接: 题目:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 代码(github):https://gi

030 Substring with Concatenation of All Words

030 Substring with Concatenation of All Words 用一个dictiionary来记录当前对应还有多少需要match的word. dict.copy()用来copy dictionary. from collections import defaultdict class Solution: # @param {string} s # @param {string[]} words # @return {integer[]} def findSubstri

[Leetcode][Python]30: Substring with Concatenation of All Words

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 30: Substring with Concatenation of All Wordshttps://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, S, and a list of words, L, that are all of the

LeetCode 030 Substring with Concatenation of All Words

题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any i

leetcode第29题--Substring with Concatenation of All Words

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

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. For example,

LeetCode题解 #3 Longest Substring Without Repeating Characters

找出字符串中没有相同字符的的最长串 注意这里的 Characters指的是字符,不是字母,就是说|/?~这样的字符都会出现,所以要用到ASCII码 最简单的方法是,从第一个字符开始,往后一个个判断,里面有没有重复的字符,如果重复了则记录下长度. 例如:abcabcbb 第一次:abc 重复于a  长度3 第二次:bca 重复与b  长度3 第三次:cab 重复与c  长度3 ...... 但这种方法很耗时 如果是 abcdefghijk这种 第一次就找到了的 abcdefghijk 但还要第二次

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

【leetcode刷题笔记】Substring with Concatenation of All Words

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