当前位置:网站首页>30. 串联所有单词的子串
30. 串联所有单词的子串
2022-07-01 03:23:00 【Sun_Sky_Sea】
30. 串联所有单词的子串
原始题目链接:https://leetcode.cn/problems/substring-with-concatenation-of-all-words/
给定一个字符串 s 和一些 长度相同 的单词 words 。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:s = “barfoothefoobarman”, words = [“foo”,“bar”]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 “barfoo” 和 “foobar” 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
输出:[]
示例 3:
输入:s = “barfoofoobarthefoobarman”, words = [“bar”,“foo”,“the”]
输出:[6,9,12]
解题思路:
在s中以窗口大小开始查找words的单词是否都包含在内,是连续的并且都用完。
代码实现:
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
# 统计words里的单词及其个数
words_dict = {
}
for word in words:
words_dict[word] = words_dict.get(word, 0) + 1
# words的单词长度,题目说明是每个单词长度是一样的
word_len = len(words[0])
# 窗口长度:单词个数 * 单词长度
windows_len = len(words) * word_len
# 给定的字符串长度
s_len = len(s)
ans = []
# 以窗口的大小为单位,在字符串s中查找,i是每个窗口的起点
for i in range(s_len - windows_len + 1):
# 每次都要用到统计的字典,并且需要减掉个数验证是否满足题目要求
# 所以用words_dict的浅复制,循环使用,等号是引用指向同一个内存地址
# deepcopy是深复制,父对象独立,子对象不变,并且需要导入import copy
temp_dict = words_dict.copy()
# j是每个窗口一个单词的终点
j = i + word_len
# 如果s中的字符串找到了words中的一个单词,并且字典中的计数还大于0的话(表示还没用完)
while s[j - word_len : j] in temp_dict and temp_dict[s[j - word_len : j]] > 0:
# 当前用过的单词的计数减1
temp_dict[s[j - word_len : j]] -= 1
# 开始下一个单词位置
j += word_len
# 如果以i为起点并且窗口内的单词都在words_dict中,那么符合条件
if sum(temp_dict.values()) == 0:
ans.append(i)
return ans
参考文献:
https://leetcode.cn/problems/substring-with-concatenation-of-all-words/solution/san-chong-fang-fa-zhu-jian-you-hua-by-hardcandy/
边栏推荐
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- The combination of applet container technology and IOT
- torch.histc
- 二叉树神级遍历:Morris遍历
- Cookie&Session
- 用小程序的技术优势发展产业互联网
- Md5sum operation
- The difference between MFC for static libraries and MFC for shared libraries
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- Valentine's Day is nothing.
猜你喜欢
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Test function in pychram
完全背包问题
岭回归和lasso回归
pytorch nn. AdaptiveAvgPool2d(1)
服务器渲染技术jsp
The combination of applet container technology and IOT
Home online shopping project
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
随机推荐
Blueprism registration, download and install -rpa Chapter 1
The shell script uses two bars to receive external parameters
排序链表(归并排序)
SEM of C language_ Tvariable type
AfxMessageBox和MessageBox的用法
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
【快捷键】
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
BluePrism注册下载并安装-RPA第一章
Ridge regression and lasso regression
Detailed list of errors related to twincat3 ads of Beifu
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
线程数据共享和安全 -ThreadLocal
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
go实现命令行的工具cli
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
静态库使用MFC和共享库使用MFC的区别
Cookie&Session
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Bilinear upsampling and f.upsample in pytorch_ bilinear