Skip to content

Commit f659ad9

Browse files
authored
Update slide_window.md
1 parent 850de84 commit f659ad9

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

advanced_algorithm/slide_window.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,41 @@ class Solution:
131131
return False
132132
```
133133

134+
```Python
135+
class Solution:
136+
def checkInclusion(self, s1: str, s2: str) -> bool:
137+
target = collections.defaultdict(int)
138+
139+
for c in s1:
140+
target[c] += 1
141+
142+
r, num_char = 0, len(target)
143+
144+
while r < len(s2):
145+
if s2[r] in target:
146+
l, count = r, 0
147+
window = collections.defaultdict(int)
148+
while r < len(s2):
149+
c = s2[r]
150+
if c not in target:
151+
break
152+
window[c] += 1
153+
if window[c] == target[c]:
154+
count += 1
155+
if count == num_char:
156+
return True
157+
while window[c] > target[c]:
158+
window[s2[l]] -= 1
159+
if window[s2[l]] == target[s2[l]] - 1:
160+
count -= 1
161+
l += 1
162+
r += 1
163+
else:
164+
r += 1
165+
166+
return False
167+
```
168+
134169
### [find-all-anagrams-in-a-string](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/)
135170

136171
> 给定一个字符串  ****和一个非空字符串  **p**,找到  ****中所有是  ****的字母异位词的子串,返回这些子串的起始索引。
@@ -175,6 +210,40 @@ class Solution:
175210
return results
176211
```
177212

213+
```Python
214+
class Solution:
215+
def findAnagrams(self, s: str, p: str) -> List[int]:
216+
w_dict = {}
217+
n_dict = {}
218+
for p_s in p:
219+
if p_s not in n_dict:
220+
n_dict[p_s] = 1
221+
w_dict[p_s] = 0
222+
else:
223+
n_dict[p_s] += 1
224+
p_len = len(p)
225+
s_len = len(s)
226+
left = 0
227+
right = 0
228+
result = []
229+
while (left+p_len-1) <= s_len and right <= (s_len-1):
230+
new_s = s[right]
231+
if not new_s in n_dict:
232+
right += 1
233+
for w_s in w_dict:
234+
w_dict[w_s] = 0
235+
left = right
236+
else:
237+
w_dict[new_s] += 1
238+
if (right-left+1) == p_len:
239+
if w_dict == n_dict:
240+
result.append(left)
241+
w_dict[s[left]] -= 1
242+
left += 1
243+
right += 1
244+
return result
245+
```
246+
178247
### [longest-substring-without-repeating-characters](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
179248

180249
> 给定一个字符串,请你找出其中不含有重复字符的   最长子串   的长度。
@@ -200,6 +269,28 @@ class Solution:
200269
return max(max_length, len(s) - l) # note that the last substring is not judged in the loop
201270
```
202271

272+
```Python
273+
class Solution:
274+
def lengthOfLongestSubstring(self, s: str) -> int:
275+
windows = {}
276+
length = len(s)
277+
left, right = 0, 0
278+
output = 0
279+
while left < length and right < length:
280+
if s[right] not in windows:
281+
windows[s[right]] = 1
282+
else:
283+
windows[s[right]] += 1
284+
while windows[s[right]] > 1:
285+
windows[s[left]] -= 1
286+
left += 1
287+
current = sum(windows.values())
288+
if current > output:
289+
output = current
290+
right += 1
291+
return output
292+
```
293+
203294
## 总结
204295

205296
- 和双指针题目类似,更像双指针的升级版,滑动窗口核心点是维护一个窗口集,根据窗口集来进行处理

0 commit comments

Comments
 (0)