当前位置:网站首页>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/
边栏推荐
- 【伸手党福利】开发人员重装系统顺序
- The combination of applet container technology and IOT
- Listener listener
- Complete knapsack problem
- Data exchange JSON
- [party benefits] jsonobject to string, leave blank
- You cannot right-click F12 to view the source code solution on the web page
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- The shell script uses two bars to receive external parameters
猜你喜欢

4、【WebGIS实战】软件操作篇——数据导入及处理

使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况

FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)

排序链表(归并排序)

Cookie&Session

bootsrap中的栅格系统

Depth first traversal of C implementation Diagram -- non recursive code

还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板

深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)

LeetCode 128最长连续序列(哈希set)
随机推荐
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Pytorch training deep learning network settings CUDA specified GPU visible
bootsrap中的栅格系统
[nine day training] content III of the problem solution of leetcode question brushing Report
ECMAScript 6.0
multiple linear regression
SEM of C language_ Tvariable type
用小程序的技术优势发展产业互联网
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
Cookie&Session
torch.histc
How to achieve 0 error (s) and 0 warning (s) in keil5
The combination of applet container technology and IOT
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Research on target recognition and tracking based on 3D laser point cloud
Cygwin的下载和安装配置
复习专栏之---消息队列
Pyramid Scene Parsing Network【PSPNet】论文阅读
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C