-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_438_0041.py
More file actions
73 lines (67 loc) · 1.9 KB
/
LeetCode_438_0041.py
File metadata and controls
73 lines (67 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
#
# 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
#
# 说明:
#
#
# 字母异位词指字母相同,但排列不同的字符串。
# 不考虑答案输出的顺序。
#
#
# 示例 1:
#
#
# 输入:
# s: "cbaebabacd" p: "abc"
#
# 输出:
# [0, 6]
#
# 解释:
# 起始索引等于 0 的子串是 "cba", 它是 "abc" 的字母异位词。
# 起始索引等于 6 的子串是 "bac", 它是 "abc" 的字母异位词。
#
#
# 示例 2:
#
#
# 输入:
# s: "abab" p: "ab"
#
# 输出:
# [0, 1, 2]
#
# 解释:
# 起始索引等于 0 的子串是 "ab", 它是 "ab" 的字母异位词。
# 起始索引等于 1 的子串是 "ba", 它是 "ab" 的字母异位词。
# 起始索引等于 2 的子串是 "ab", 它是 "ab" 的字母异位词。
#
# Related Topics 哈希表
# leetcode submit region begin(Prohibit modification and deletion)
from collections import Counter
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
m = Counter(p)
counter = len(m)
i, j, n, d = 0, 0, len(s), len(p)
res = []
while j < n:
if s[j] in m:
m[s[j]] -= 1
if m[s[j]] == 0: # 条件需根据具体情况变化
counter -= 1
j += 1
while counter == 0: # 条件需根据具体情况变化
if j - i == d:
res.append(i)
if s[i] in m:
m[s[i]] += 1
if m[s[i]] > 0: # 条件需根据具体情况变化
counter += 1
i += 1
return res
# leetcode submit region end(Prohibit modification and deletion)
print(Solution().findAnagrams("baa",
"aa"))