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中这个单词出现的个数,此子句不符合要求

 class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
res = []
length = len(words[0])
num = len(words)
l = length*num
lens = len(s)
wdict = {}
sset = set()#建立集合,收集所有已经符合条件的字句,减少判断次数
for word in words:
wdict[word] = 1+(wdict[word] if word in wdict else 0)
first_char = set(w[0] for w in words)#建立集合收集所有单词中的第一个字母,减少isRequired的次数
for i in range(lens-l+1):
if s[i] in first_char:
tmp = s[i:i+l]
if tmp in sset or self.isRequired(tmp,wdict,length):
sset.add(tmp)
res.append(i)
return res
def isRequired(self,s,wdict,length):
comp = {}
i = 0
while(i<len(s)-length+1):
tmp = s[i:i+length]
if tmp not in wdict:
return False
else:
comp[tmp] = 1+(comp[tmp] if tmp in comp else 0)
if comp[tmp]>wdict[tmp]:
return False
i += length
return True

    

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

  1. 【一天一道LeetCode】#30. Substring with Concatenation of All Words

    注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...

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

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  3. 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 sta ...

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

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  6. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. 【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 longes ...

  8. 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

  9. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

随机推荐

  1. sqlclr创建表值函数案例

    ----------------------------------------------:定义表值类型:(1.一定返回的是IEnumerable2.一定带参数3.一定有FillRowMethodN ...

  2. asp.net mvc使用validate.js验证 若name属性包含特殊字符则加上双引号即可

    rules: {                    "Can.CName": {                        required: true,          ...

  3. SOLID 设计原则 In C# 代码实现

    [S] Single Responsibility Principle (单一职责原则) 认为一个对象应该仅只有一个单一的职责 namespace SingleResponsibilityPrinci ...

  4. web 富文本编辑器总结

    前言 富文本编辑器,就是除了能输入不同的文本之外,还可以之间粘贴图画等其他的多媒体信息.也可说是所见即所得的编辑器. 目前可以使用的编辑器有很多, 在网络上有找到这样一份比较表格: 编辑器 产地 稳定 ...

  5. Swift之函数语法详解

    函数 函数是用来完成特定任务的独立的代码块.你给一个函数起一个合适的名字,用来标识函数做什么,并且当函数需要执行的时候,这个名字会被“调用”. Swift 统一的函数语法足够灵活,可以用来表示任何函数 ...

  6. IP数据报是如何在网络中转发的?

    首先发送方抽取目的站的网络前缀,来判断是否目的站是否位于同一网络上,如果在二者有相同的网络前缀,则,直接使用直接交付的方式, 否则,使用由路由器互连的间接交付,这要借助IP路由表,采用表驱动法,路由表 ...

  7. AngularJS的初始化

    AngularJS的初始化 本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过 ...

  8. 字符串匹配算法 之 基于DFA(确定性有限自动机)

    确定有限自动机定义:http://en.wikipedia.org/wiki/Deterministic_finite_automaton 自动机在字符串匹配中的应用 #include<stdi ...

  9. AspNet MVC3中过滤器 + 实例

    AspNet MVC3中过滤器 + 实例 过滤器在请求管线注入额外的逻辑,提供简单优雅的方法实现横切点关注(AOP),例如日志,授权,缓存等应用.通过AOP可以减少在实际的业务逻辑中参杂过多非直接业务 ...

  10. UML类图的常见关系1

    设计模式之UML类图的常见关系(一)   本篇会讲解在UML类图中,常见几种关系: 泛化(Generalization),依赖(Dependency),关联(Association),聚合(Aggre ...