【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中这个单词出现的个数,此子句不符合要求
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的更多相关文章
- 【一天一道LeetCode】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- 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 ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【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 ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
随机推荐
- SongTaste音乐下载器
SongTaste音乐下载器 Songtaste是一个非常好的音乐推荐网站, 奈何和duomi搅合在一起, 导致下载音乐非常的麻烦, 现在写了一个简单的"下载器", 通过它可以下载 ...
- Node填坑教程——HelloWorld
环境安装(极简): Node需要的环境可以说及其简单,也可以说及其复杂.为什么这么说呢? 如果里只需要运行环境那么到Node官网下载一个包就行了.里面自带npm管理工具,这是包管理工具,以后会频繁的使 ...
- 代码阅读软件kscope源码安装指导
安装 kscope-1.6.2 1. ./configure --without-arts --prefix=/soft/kscope-1.6.2 (I customize the installi ...
- HttpClient 检索与获取过程数据
使用 HttpClient 检索与获取过程数据 对于System.Net.Http的学习(一)——System.Net.Http 简介 对于System.Net.Http的学习(二)——使用 Ht ...
- 随机函数Surprising
之前写了个用来抽取1-54号的随机函数,发现30-40出现的情况很大,就在果壳上提问了一下//听取了某个大神的建议循环了10000次之后惊喜的发现这样写出现了一大堆相同的数字! 之后有个很神大牛解答了 ...
- Introsort(内观排序)
.NET 4.5 这个版本的Array.Sort更改了STL的内观排序算法,那相对于快速排序内观排序到底有什么优化过的呢? 根据维基百科所说,这个排序算法首先从快速排序开始,当递归深度超过一定深度(深 ...
- Weka开发[4]-特征选择
特征选择,我对这一部分也不熟,大概讲一下,用AttributeSelection进行特征选择,它需要设置3个方面,第一:对属性评价的类(自己到Weka软件里看一下,英文Attribute Evalua ...
- Clob类型转换成String类型
oracle中表结构如下: create table GRID_RESOURCE ( ID VARCHAR2(50), CNNAME VARCHAR2(50), TYPE VARCHAR2(50), ...
- 鸟哥的LINUX私房菜基础篇第三版 阅读笔记 一
1. Linux的档案权限与目录配置 一.基础知识: a.分为三类,拥有者(owner).群组(group).其他人(other) b.三个核 ...
- Profession ASP.NET MVC 2.0 NerdDinner示例可运行源码
最近一段时间在看JonGalloway等著作的<Profession ASP.NET MVC 2.0>.本书并没有按照常规的大部头书籍那样,按部就班的介绍MVC的概念等,而是在第一章直接引 ...