Skip to content

Commit 066fa47

Browse files
committed
🐱(sliding-window): 567. 字符串的排列 add python code
1 parent c23b866 commit 066fa47

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/algorithm/sliding-window/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,43 @@ class Solution:
112112
- 在 s2 上放置 "滑动窗口",计算与 s1 等长的字串是否拥有与 s1 相同的字符数
113113

114114
```python
115-
115+
class Solution:
116+
def checkInclusion(self, s1: str, s2: str) -> bool:
117+
s1_length = len(s1)
118+
s2_length = len(s2)
119+
if s1_length > s2_length:
120+
return False
121+
122+
count1 = [0 for _ in range(26)]
123+
count2 = [0 for _ in range(26)]
124+
ord_a = ord('a')
125+
126+
# s1 字符个数记录
127+
for c in s1:
128+
count1[ord(c) - ord_a] += 1
129+
130+
# s2 第一个窗口字符记录
131+
for i in range(s1_length):
132+
count2[ord(s2[i]) - ord_a] += 1
133+
134+
"""
135+
是否匹配
136+
"""
137+
def is_match(count1, count2):
138+
for i in range(len(count1)):
139+
if count1[i] != count2[i]:
140+
return False
141+
return True
142+
143+
if is_match(count1, count2):
144+
return True
145+
146+
for i in range(s1_length, s2_length):
147+
left = ord(s2[i - s1_length]) - ord_a
148+
count2[left] -= 1
149+
count2[ord(s2[i]) - ord_a] += 1
150+
if is_match(count1, count2):
151+
return True
152+
153+
return False
116154
```

0 commit comments

Comments
 (0)