【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, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

题意:找出所给列表words中的单词相接的子句在s中出现的索引。

注意:words中的单词可能重复出现

思路:1.建立一个字典wdict,统计words中所有单词的个数

  2.判断s中所有可能的子句是否符合要求

    1)判断字据中每个单词是否出现在wdict中,没有的话,此子句不符合要求

    2)子句符合要求的话,加入新的属于子句的字典,如果子句中某个单词出现的次数超过wdict中这个单词出现的个数,此子句不符合要求

 1 class Solution(object):
 2     def findSubstring(self, s, words):
 3         """
 4         :type s: str
 5         :type words: List[str]
 6         :rtype: List[int]
 7         """
 8         res = []
 9         length = len(words[0])
10         num = len(words)
11         l = length*num
12         lens = len(s)
13         wdict = {}
14         sset = set()#建立集合,收集所有已经符合条件的字句,减少判断次数
15         for word in words:
16             wdict[word] = 1+(wdict[word] if word in wdict else 0)
17         first_char = set(w[0] for w in words)#建立集合收集所有单词中的第一个字母,减少isRequired的次数
18         for i in range(lens-l+1):
19             if s[i] in first_char:
20                 tmp = s[i:i+l]
21                 if tmp in sset or self.isRequired(tmp,wdict,length):
22                     sset.add(tmp)
23                     res.append(i)
24         return res
25     def isRequired(self,s,wdict,length):
26         comp = {}
27         i = 0
28         while(i<len(s)-length+1):
29             tmp = s[i:i+length]
30             if tmp not in wdict:
31                 return False
32             else:
33                 comp[tmp] = 1+(comp[tmp] if tmp in comp else 0)
34                 if comp[tmp]>wdict[tmp]:
35                     return False
36             i += length
37         return True
38
39     

    

时间: 2024-08-06 23:38:56

【LeetCode】30. Substring with Concatenation of All Words的相关文章

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

[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 HashTable 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, giv

【LeetCode】Longest Substring Without Repeating Characters 解题报告

[题意] Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest

【Leetcode】Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)

Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = “eceba”, T is "ece" which its length is 3. 这个题很显然使用双指针进行遍历的,beg

【leetcode】Longest Substring Without Repeating Characters (middle)

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"