【LeetCode每天一题】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.
Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"] Output: [0, 9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.
Example 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"] Output: []
思路
在一开始做这道题时,想着从头到尾一直遍历使用暴力破解法来解决,但是在编码完成之后,一直不通过。最终看了以下答案。发现整体思路都是一样的只不过在细节上处理上更加厉害。
这道题的整体思路是先将words列表中的单词转化为字典,然后进行遍历来判断是否存在字符串中。详细见代码。
解决代码
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
from collections import Counter
if not s or not words:
return []
word_size = len(words[0]) # 因为每一个单词长度一样,求出其中一个单词的长度
word_map = Counter(words) # 将words列表中的单词进行计数统计
results = [] # 存储结果
num_word = len(words) # 单词的总数
list_size = word_size*num_word # 所有单词的总长度
for i in range(len(s)-list_size+1): # 只需要遍历到 s的长度减去单词的总长度的位置就行了,因为后面的不会满足条件。
seen = dict(word_map) # 临时字典,存储的是每个单词的出现次数
word_used = 0
for j in range(i, i+list_size, word_size): # 从 第i个位置开始,到所有单词总长度的位置,意思是判断这一部分是否满足条件。
sub_str = s[j:j+word_size] # 在s中提取相应单词长度的字符串。
if sub_str in seen and seen[sub_str] >0: # 如果提出出的字符串在seen字典中,并且相应的值不为0
seen[sub_str] -= 1 # 相应字符串的值减一,
word_used += 1 # 匹配的数目加一
else:
break # 如果不满足,直接返回,后面不会存在满足添加的可能。
if word_used == num_word: # 相等则表明所有的都匹配完。
results.append(i) # 存储下标
return results
【LeetCode每天一题】Substring with Concatenation of All Words(具备列表中所有单词的字串)的更多相关文章
- leetcode第29题--Substring with Concatenation of All Words
problem: You are given a string, S, and a list of words, L, that are all of the same length. Find al ...
- 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 ...
- LeetCode 笔记系列七 Substring with Concatenation of All Words
题目:You are given a string, S, and a list of words, L, that are all of the same length. Find all star ...
- Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置
问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...
- Longest Substring Without Repeating Characters,求没有重复字符的最长字串
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode随缘刷题之截断句子
这道题相对比较简单.正好最近学到StringBuilder就用了. package leetcode.day_12_06; /** * 句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前 ...
- 【leetcode刷题笔记】Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- LeetCode:Substring with Concatenation of All Words (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- 乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words
乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words 一.前言 这次我们还是找字符串的索引,不过,需要将另一个字符串列表中的 ...
随机推荐
- D - Area of Mushroom
Teacher Mai has a kingdom with the infinite area. He has n students guarding the kingdom. The i-th s ...
- ubuntu-18.04 root登录图形界面失败问题解决方案
一.设置root密码 二.进入/etc/pam.d目录 主要修改两个文件(圈了红色框框),记得命令行下切换root账户(sudo -i)进行vim修改,刚安装的ubuntu没有vim支持,请根据提示进 ...
- Flask web开发之路二
今天创建第一个flask项目,主app文件代码如下: # 从flask这个框架导入Flask这个类 from flask import Flask #初始化一个Flask对象 # Flasks() # ...
- hdfs 操作 入门api
获取分布式文件系统 // 获取文件系统 @Test public void getFileSystem() throws Exception{ Configuration configuration ...
- [administrative][CentOS][NetworkManager] 万恶的NetworkManager到底怎么用
这好像是第三次不得不去学会NetworkManager的用法,可是它真的很难用.社区里无人不吐槽. 然而,还是要用! 这次从redhat的文档入手,也许可以成功 --! https://access. ...
- 请教神牛_字符串hash
针对字符串hash 我早就听闻可以暴力的干一些事情. 比如 可以... 很多很多 实现O(n)求出 模式串在文本串出现的次数. 但是我不会这什么hash. 我会自然溢出字符串hash 嘿嘿 unsig ...
- el表达式获取对象属性值 返回值类型
实现 数字页码时 遇到的一个问题. 后端servlet 在request.setAttribute("page",page); page 为pagebean的实例对象,pagebe ...
- Java之旅_面向对象_接口
参考摘自:http://www.runoob.com/java/java-interfaces.html 接口(interface)在Java中是一个抽象类型,是抽象方法的集合. 一个类通过imple ...
- shiro默认过滤器
- SSL连接分为两个阶段:握手和数据传输阶段
一.SSL概述SSL连接分为两个阶段:握手和数据传输阶段.握手阶段对服务器进行认证并确立用于保护数据传输的加密密钥,必须在传输任何应用数据之前完成握手.一旦握手完成,数据就被分成一系列经过保护的记录进 ...