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

    3.7.37 ICAP_SUPPORTEDSIZESType:TW_UINT16Supported Messages:MSG_GET - TW_ENUMERATIONMSG_GETCURRENT - ...

  2. PCL Save VTK File With Texture Coordinates 使用PCL库来保存带纹理坐标的VTK文件

    我之前有一篇博客Convert PLY to VTK Using PCL 1.6.0 or PCL 1.8.0 使用PCL库将PLY格式转为VTK格式展示了如何将PLY格式文件转化为VTK格式的文件, ...

  3. 壁虎书5 Support Vector Machine

    SVM is capable of performing linear or nonlinear classification,regression,and even outlier detectio ...

  4. 英语专业出身也要走向python

    这两年一直徘徊在学习python和放弃python的道路上不断的徘徊,今年终于没有在蹉跎下去,选择了开始新的自我挑战,零基础开始学习python. 作为一名英语专业毕业的文科生,学习编程还是相对有些困 ...

  5. Echarts 的Formatter的回调函数

    option = { tooltip: { trigger: 'axis', formatter: function (params,ticket,callback) { let res = para ...

  6. [No0000DB]C# FtpClientHelper Ftp客户端上传下载重命名 类封装

    using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...

  7. python--列表、字典、元组、集合对比

    数据类型# 计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数据类型.# a:整形和浮点型(正数和负数)# b:布尔类型(true,fals ...

  8. screen基本用法

    当某些命令执行时间过长或者某些程序关闭shell后自动断开时,就能使用screen 1.安装yum -y install screen 2.输入screen命令进入screen控制台 3.输入scre ...

  9. JNI 入门

    1.http://cherishlc.iteye.com/blog/1756762 Android 学习笔记--利用JNI技术在Android中调用.调试C++代码 2.http://my.eoe.c ...

  10. 内部排序->其它->地址排序(地址重排算法)

    文字描述 当每个记录所占空间较多,即每个记录存放的除关键字外的附加信息太大时,移动记录的时间耗费太大.此时,就可以像表插入排序.链式基数排序,以修改指针代替移动记录.但是有的排序方法,如快速排序和堆排 ...