Leetcode——30.与所有单词相关联的字串【##】
@author: ZZQ
@software: PyCharm
@file: leetcode30_findSubstring.py
@time: 2018/11/20 19:14
题目要求:
给定一个字符串 s 和一些长度相同的单词 words。在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:
s = "barfoothefoobarman",
words = ["foo","bar"]
输出: [0,9]
解释: 从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:
s = "wordgoodstudentgoodword",
words = ["word","student"]
输出: []
这个案例有点迷,题目明明说words里面的单词都是等长的。。。
思路:
方法一:
拿到题目,我们需要做的是找出匹配words中所有单词的子串的下标。
首先,我们用一个hashmap_m1去记录words中出现的单词及其个数,
然后,开始遍历,依次比较从i,i+words_len的所有子串是否匹配words里单词的某种自由组合,当i的值>s_len-words_num*word_len时,剩余的子串也不够匹配的,i至此停止遍历。
在内层循环中, 我们设置一个新的hashmap_m2来存储当前子串中匹配到的单词及其个数。
当该单词不在hashmap_m1中时,匹配失败,break
当hashmap_m2中该单词的个数超出hashmap_m1中该单词的个数时,匹配失败,break
当j刚好等于words_nums时,说明匹配成功,则将当前的下标i存入ans中。
AC代码如下:
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
if s == "" or words == []:
return []
ans = []
hash_m1 = {}
words_num = len(words)
word_len = len(words[0])
s_len = len(s)
if s_len < words_num:
return []
for word in words: # 记录每个单词出现的次数
if word not in hash_m1:
hash_m1[word] = 1
else:
hash_m1[word] += 1
for i in range(s_len-words_num*word_len+1):
hash_m2 = {}
j = 0
while j < words_num: # 需要匹配的单词的个数
current_word = s[i+j*word_len:i+(j+1)*word_len]
if current_word in hash_m1:
if current_word not in hash_m2:
hash_m2[current_word] = 1
else:
hash_m2[current_word] += 1
if hash_m2[current_word] > hash_m1[current_word]:
break
else:
break
j += 1
if j == words_num:
ans.append(i)
return ans
方法二:
但是这种方法比较慢,看了网上大神的解法,于是有了方法二。
这是一个可以达到O(n)的方法。i不是一个字母一个字母的遍历,而是一个单词一个单词的遍历。
比如说:
当前words中所有单词的长度都为3,则遍历的方法是先遍历0,3,6,9,12...,然后扫描1,4,7,10,13,...,再扫描2,5,8,11,14,...,也就是说外层循环i只需要遍历【0,3】即可。
我们先设置一个hashmap_m1用于保存words中每个单词出现的个数,然后,对于某个i,设置left来记录某个字串的起始下标,设置count来记录匹配成功的单词个数,设置临时变量k遍历【i,len(s)】之后的所有字母,但此时不依次遍历,而是一个单词一个单词的遍历。设置临时字典hashmap_m2来存储在当下循环中匹配到的单词及其个数。
内层循环中,判断当前的current_word是否在hashmap_m1:
如果在,则将hashmap_m2中该单词的个数加1,同时判断hashmap_m2中current_word的个数是否超过hashmap_m1中current_word,如果没有超,则将count加1,否则,说明此时匹配到的单词个数不符合words里的单词个数,则从hashmap_m2中去掉最前面的一个单词,然后将count减1,同时将left后移word_len个单位,直至hashmap_m2【current_word】<=hashmap_m1【current_word】。 当count的值与words中单词的个数相同时,说明匹配成功,则将该起始下标left添加到ans中,然后将left后移word_len个单位,同时将count减1。
如果current_word不在hashmap_m1则,则说明之前的所有匹配都失败,此时应该将hashmap_m2清零,同时将left后移到j+word_len的位置(重新开始匹配)。
AC代码如下:
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
if s == "" or words == []:
return []
ans = []
hash_m1 = {}
words_num = len(words)
s_len = len(s)
if s_len < words_num:
return []
for word in words:
if word not in hash_m1:
hash_m1[word] = 1
else:
hash_m1[word] += 1
word_len = len(words[0])
for i in range(word_len):
left = i # 记录起始下标
count = 0 #记录匹配成功的个数
hash_m2 = {} # 记录匹配到的单词及其个数
j = i # 从i开始往后遍历
while j <= s_len - word_len: # 循环停止条件是遍历到最后一个单词所在位置
current_word = copy.deepcopy(s[j:j + word_len]) # 当前单词
if current_word in hash_m1: # 如果单词在hash_m1 中
if current_word not in hash_m2: # 将其加入hash_m2
hash_m2[current_word] = 1
else:
hash_m2[current_word] += 1
if hash_m2[current_word] <= hash_m1[current_word]: # 如果该单词在hash_m2中的个数小于等于其在hash_m1中的个数,将count+1
count += 1
else: # 否则将hash_m2中最先匹配到的单词剔除,直至hash_m2[current_word] <= hash_m1[current_word]
while hash_m2[current_word] > hash_m1[current_word]:
temp_word = s[left:left+word_len]
hash_m2[temp_word] -= 1
if hash_m2[temp_word] < hash_m1[temp_word]:
count -= 1
left += word_len
if count == words_num: # 匹配成功
if left not in ans:
ans.append(left)
hash_m2[s[left:left + word_len]] -= 1 # left后移一个单词的长度,hash_m2中最先匹配到的单词移除一个
count -= 1
left += word_len
else: # 当前单词不在hash_m1中,匹配失败,重新开始匹配,count清零,left设置到当前位置
hash_m2.clear()
count = 0
left = j + word_len
j += word_len
return ans
Leetcode——30.与所有单词相关联的字串【##】的更多相关文章
- [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)
30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...
- 30. 与所有单词相关联的字串、java实现
题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符, ...
- Leetcode 30.与所有单词相关联的子串
与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
- LeetCode(30):与所有单词相关联的字串
Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
- [Swift]LeetCode30. 与所有单词相关联的字串 | 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 ...
- 030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...
随机推荐
- 5.Dubbo2.5.3泛化引用和泛化实现
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所有POJO均用Ma ...
- CSS鼠标经过另类做法
HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- xpath获取带注释的text
from lxml import etree html_str = """<div id="box1">this from blog.cs ...
- 使用Tensorflow训练自己的数据
训练自己的数据集(以bottle为例): 1.准备数据 文件夹结构: models ├── images ├── annotations │ ├── xmls │ └── trainval.txt ...
- [转]C#操作INI文件
在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设 ...
- 在Node.js中操作文件系统(一)
在Node.js中操作文件系统 在Node.js中,使用fs模块来实现所有有关文件及目录的创建,写入及删除操作.在fs模块中,所有对文件及目录的操作都可以使用同步与异步这两种方法.比如在执行读文件操作 ...
- Node基础知识点--学习笔记(一)
一:建立http服务器: 在D盘建立一个文件夹node,放入app.js,代码如下: var http = require('http'); http.createServer(function(re ...
- openstack vm ping 114.114.114.114
route add -net 0.0.0.0 netmask 0.0.0.0 gateway 192.168.0.131 route del -net 0.0.0.0 netmask 0.0.0.0 ...
- Hadoop大数据平台构建
基础:linux常用命令.Java编程基础大数据:科学数据.金融数据.物联网数据.交通数据.社交网络数据.零售数据等等. Hadoop: 一个开源的分布式存储.分布式计算平台.(基于Apache) H ...
- 小R的烦恼 BZOJ3280
分析: 一开始一直Wa,发现是建图建错了,必须得拆点. S连i,流量为a[i],费用为0,i+n连T,流量同上,费用为0,之后i连i+1费用为0,流量为inf,之后S连n*2+i,流量为li,费用为0 ...