@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.与所有单词相关联的字串【##】的更多相关文章

  1. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  2. 30. 与所有单词相关联的字串、java实现

    题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符, ...

  3. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  4. LeetCode(30):与所有单词相关联的字串

    Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  5. [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 ...

  6. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  7. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  8. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  9. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

随机推荐

  1. jQuery----各版本

    jQuery是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作.由John Resig在2006年1月的BarCamp NYC上发布第一个版本.目前是由 Dave M ...

  2. SAP跟踪前台操作导致的后台查询语句

    SAP跟踪前台操作导致的后台查询语句,通过这个可以查看前台对应了后台的数据库表,然后可以通过se11查看表内容,也可以删除表内容. 在sap升级的时候,首先需要拷贝正式的sap系统,然后将拷贝的系统中 ...

  3. 利用单例模式设计数据库连接Model类

    之前在<[php]利用php的构造函数与析构函数编写Mysql数据库查询类>(点击打开链接)写过的Mysql数据库查询类还不够完美,利用<[Java]单例模式>(点击打开链接) ...

  4. android 解决小米手机上选择照片路径为null情况

    昨天测试帅哥说他手机选择图库崩溃了,这是一个上传头像的功能,相信很多应用都有这个功能,于是我就把手机拿过来打log看了下返回的路径 为null,在网上搜索了下解决方案,现在把解决方案记录下: 这是在o ...

  5. NodeHandles

    os::NodeHandle类有两个作用: 第一.它在roscpp程序内提供了一种RAII(Resource Acquisition Is Initialization)类型式启动和关闭内部节点的方法 ...

  6. leetcode322—Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. 使用react-navigation时报错:undefined is not an object (evaluating rngesturehandlermodule.direction)

    问题: 使用react-navigation时报错:undefined is not an object (evaluating rngesturehandlermodule.direction). ...

  8. 【转】软件质量之道:SourceMonitor

    转:https://blog.csdn.net/feng_ma_niu/article/details/40594799 SourceMonitor是一个源代码衡量工具,由http://www.cam ...

  9. 大数据入门第十四天——Hbase详解(三)hbase基本原理与MR操作Hbase

    一.基本原理 1.hbase的位置 上图描述了Hadoop 2.0生态系统中的各层结构.其中HBase位于结构化存储层,HDFS为HBase提供了高可靠性的底层存储支持, MapReduce为HBas ...

  10. OWASP移动安全漏洞Top 10

    •脆弱的服务器端安全控制 在OWASP排第一的漏洞是“脆弱的服务器端安全控制”,顾名思义,就是没有以一个安全的方式从移动应用程序向服务器端发送数据,或在发送数据时暴露了一些敏感的API.例如,考虑对一 ...