Skip to content

Commit bbf0abf

Browse files
author
lucifer
committed
1 parent 3704f7b commit bbf0abf

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ leetcode 题解,记录自己的 leetcode 解题之路。
241241

242242
#### 困难难度
243243

244-
- [0004.median-of-two-sorted-array](./problems/4.median-of-two-sorted-array.md) 🆕
244+
- [0004.median-of-two-sorted-array](./problems/4.median-of-two-sorted-array.md)
245245
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
246246
- [0025.reverse-nodes-in-k-group](./problems/25.reverse-nodes-in-k-groups-cn.md) 🆕
247-
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
247+
- [0030.substring-with-concatenation-of-all-words](./problems/30.substring-with-concatenation-of-all-words.md)
248+
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md)
248249
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
249250
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
250251
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## 题目地址(30. 串联所有单词的子串)
2+
3+
https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/description/
4+
5+
## 题目描述
6+
7+
```
8+
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
9+
10+
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
11+
12+
 
13+
14+
示例 1:
15+
16+
输入:
17+
s = "barfoothefoobarman",
18+
words = ["foo","bar"]
19+
输出:[0,9]
20+
解释:
21+
从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
22+
输出的顺序不重要, [9,0] 也是有效答案。
23+
示例 2:
24+
25+
输入:
26+
s = "wordgoodgoodgoodbestword",
27+
words = ["word","good","best","word"]
28+
输出:[]
29+
30+
31+
```
32+
33+
## 思路
34+
35+
本题是要我们找出 words 中`所有单词按照任意顺序串联`形成的单词中恰好出现在 s 中的索引,因此顺序是不重要的。换句话说,我们只要统计每一个单词的出现情况即可。以题目中 s = "barfoothefoobarman", words = ["foo","bar"] 为例。 我们只需要统计 foo 出现了一次,bar 出现了一次即可。我们只需要在 s 中找到同样包含一次 foo 和一次 bar 的子串即可。由于 words 中的字符串都是等长的,因此编码上也会比较简单。
36+
37+
1. 我们的目标状态是 Counter(words),即对 words 进行一次计数。
38+
2. 我们只需从头开始遍历一次数组,每次截取 word 长度的字符,一共截取 words 长度次即可。
39+
3. 如果我们截取的 Counter 和 Counter(words)一致,则加入到 res
40+
4. 否则我们继续一个指针,继续执行步骤二
41+
5. 重复执行这个逻辑直到达到数组尾部
42+
43+
## 关键点解析
44+
45+
- Counter
46+
47+
## 代码
48+
49+
- 语言支持:Python3
50+
51+
```python
52+
from collections import Counter
53+
54+
55+
class Solution:
56+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
57+
if not s or not words:
58+
return []
59+
res = []
60+
n = len(words)
61+
word_len = len(words[0])
62+
window_len = word_len * n
63+
target = Counter(words)
64+
i = 0
65+
while i < len(s) - window_len + 1:
66+
sliced = []
67+
start = i
68+
for _ in range(n):
69+
sliced.append(s[start:start + word_len])
70+
start += word_len
71+
if Counter(sliced) == target:
72+
res.append(i)
73+
i += 1
74+
return res
75+
```

0 commit comments

Comments
 (0)