【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 --------------------------------------- ...
随机推荐
- Oracle Database Instant Client 11g 32位和64位 安装包发布
Oracle Database Instant Client 11g 32位和64 Oracle Database Instant Client 11g 11.2.0.3.0 Instant Clie ...
- QQ三方登录步骤详解
首先,登录QQ互联:http://connect.qq.com/intro/login ,注册成为开发者 选择申请加入,并创建你的应用. 创建成功后可以获取到appid和appkey 在网站的主页引 ...
- hdu 1239 Calling Extraterrestrial Intelligence Again (暴力枚举)
Calling Extraterrestrial Intelligence Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 基于python的《Hadoop权威指南》一书中气象数据下载和map reduce化数据处理及其可视化
文档内容: 1:下载<hadoop权威指南>中的气象数据 2:对下载的气象数据归档整理并读取数据 3:对气象数据进行map reduce进行处理 关键词:<Hadoop权威指南> ...
- REDGATE又一好用的脚本工具
REDGATE又一好用的脚本工具 REDGATE又一好用的脚本工具 先说明一下:这个工具是免费的 下载地址:http://www.red-gate.com/products/dba/sql-scr ...
- c#计算2个字符串的相似度
直接来代码 public static float levenshtein(string str1, string str2) { //计算两个字符串的长度. int len1 = str1.Leng ...
- Google C++测试框架系列:入门
Google C++测试框架系列:入门 原始链接:V1_6_Primer 注 GTest或者Google Test: Google的C++测试框架. Test Fixtures: 这个词实在找不到对应 ...
- 使用元组Tuple,返回多个不同类型的值
记得我在不知道Tuple时,如果想实现调用某个函数时,返回多个值,则需要使用ref或者out了. string name = ""; int result= GetInfo(ref ...
- jQuery表格排序组件-tablesorter
一.引入文件 <script type="text/javascript" src="js/jquery.js"></script> & ...
- jQuery实现返回顶部功能
整理两个实现功能,一个是右下角的返回顶部,一个是右侧的返回顶部,分别如图 第一种实现 一.JSP或HTML(主体结构) 在body中添加 <body id=" ...