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(具备列表中所有单词的字串)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置

    问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...

  5. Longest Substring Without Repeating Characters,求没有重复字符的最长字串

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  6. LeetCode随缘刷题之截断句子

    这道题相对比较简单.正好最近学到StringBuilder就用了. package leetcode.day_12_06; /** * 句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前 ...

  7. 【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 ...

  8. 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 ...

  9. 乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words

    乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words 一.前言    这次我们还是找字符串的索引,不过,需要将另一个字符串列表中的 ...

随机推荐

  1. 10.17正式开发stark项目(二)

    2018-10-17 11:09:48 orm补充参考连接: https://www.cnblogs.com/yuanchenqi/articles/8963244.html model 进阶 参考连 ...

  2. Python2.7编译失败 Failed to build these modules:_curses_panel _hashlib _ssl

    1:*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.0: cannot open sha ...

  3. zTree更新自定义标签>>>

    zTree>>>>>>>>>>>>>>>>>>>>>>>> ...

  4. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

  5. 安装ReactNative开发IDE

    https://blog.csdn.net/u014484863/article/details/51554428 https://github.com/reactnativecn/react-nat ...

  6. PHP之fopen wrappers模块

    一.fopen wrappers模块的配置 ①.allow_url_fopen boolean //出于安全性考虑,此选项只能在 php.ini 中设置 //本选项激活了 URL 形式的 fopen ...

  7. mysql缓存分析

  8. day1_接口测试基础

    一.什么是接口: 接口:一般分为两种,程序内部接口和程序对外接口 系统对外接口:系统与外部沟通,比如我们平时用的app,网站进行数据处理的时候都是通过接口调用后端服务器的数据. 程序内部接口:程序内部 ...

  9. LeetCode 509 Fibonacci Number 解题报告

    题目要求 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, su ...

  10. Anaconda 虚拟环境安装及应用

    首先要安装Anaconda 下载网址:https://www.anaconda.com/distribution/#download-section      Miniconda下载网址:https: ...