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. Servlet3.0 multipart 文件上传技术

    Servlet3.0 javaConfig配置 传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代 ...

  2. sometimes we should use "disable fork" instead of "disable block_name"

    A disable named block statement stops the execution of all blocks with that same name in all threads ...

  3. Flask web开发之路十二

    ge请求和post请求 ### get请求和post请求:1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放 ...

  4. 初窥scrapy爬虫

    2017-10-30  21:49:55 前言: 初步使用scrapy爬虫框架,爬取各个网站信息 系统环境: 64位win10系统,装有64位python3.6,IDE为pycharm,使用cmd命令 ...

  5. textarea 标签 属性

    textarea 标签 resize: vertical; 禁止横向拉伸 x resize: horizontal; 禁止竖向拉伸 y resize: none; 去掉拖动按钮

  6. Android Studio 工具窗口浮动与布局恢复【申明:来源于网络】

    Android Studio 工具窗口浮动与布局恢复[申明:来源于网络] http://bbs.chinaunix.net/thread-4182438-1-1.html

  7. java8集合--LinkedList纯源码

    package Queue; import java.util.*; import java.util.function.Consumer; /** * 双端队列主要实现list接口和Deque接口, ...

  8. PE/ELF文件里面存的是什么呢?

    malloc分配的空间是连续的吗?PE/ELF文件里面存的是什么呢?

  9. mail 发送email

    (一)首先安装ssmpt和mailutils: sudo apt-get install ssmtp mailutils (二)接下来编辑配置文件sudo gedit /etc/ssmtp/ssmtp ...

  10. js如何判断哪个按钮被点击了?

    用事件委托,然后判断target,代码如下: $(docuement).on('click',function(event){ event.target... }) 例如:点击.c1之外任意地方的时候 ...