【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 一.前言 这次我们还是找字符串的索引,不过,需要将另一个字符串列表中的 ...
随机推荐
- Python scipy 计算短时傅里叶变换(Short-time Fourier transforms)
计算短时傅里叶变换(STFT) scipy.signal.stft(x,fs = 1.0,window ='hann',nperseg = 256,noverlap = None,nfft = Non ...
- Sequential Minimal Optimization (SMO) 算法
SVM 最终关于 $a$ 目标函数为凸优化问题,该问题具有全局最优解,许多最优化算法都可以解决该问题,但当样本容量相对很大时,通常采用 SMO 算法(比如 LIBSVM),该算法为启发式算法,考虑在约 ...
- G - Line of Sight
来源poj2074 An architect is very proud of his new home and wants to be sure it can be seen by people p ...
- centos7 LANMP 安装
没有开头语. 操作系统:CentOs7.6 64位. Nginx:系统自带 nginx1.12.2包. Mysql:系统自带 MariaDB 5.6 ,更换为 Mysql5.6 PHP:系统php5. ...
- solus 系统 - 安装编译工具
将会安装 gcc,make等工具 sudo eopkg install -c system.devel VirtualBox工具 https://solus-project.com/articles/ ...
- python中的一个现象,db.commit和db.commit()
假设有一个表,有自增字段,在开发环境中(sublime/Liclipse等)执行insert语句时,如果调用db.commit,那么数据库中不会有这条记录,但也不报错,再次插入成功时,自增自段加1. ...
- Exception 05 : Could not instantiate id generator
异常名称: Could not instantiate id generator 异常截图: 异常原因:Sequence不支持mysql数据库 Sequence支持的是有序列的数据库,此时可以将ora ...
- JavaScript面向对象之创建类和方法
一,js使用函数来定义类而不是像别的编程语言一样通过关键字class来定义,通过类本身(this)和原型(prototype)来完成面对对象编程! 示例1, //创建ElectronicSignatu ...
- 类中的函数带有self,不带self的区别
1.类里函数不带self,这是我们调用类里的函数直接用类名.函数名() class shop(object): def scan_goods(): #括号内不带self print('浏览商品') d ...
- CSDN论坛 > Delphi > VCL组件开发及应用 DBLookupComboBox用法
(1)DataSource属性 该属性用于连接要编辑数据的主表数据源(2)DataField属性 该属性用于指定要编辑的数据字段名(3)ListSource属性 . 该属性用于 ...