题目地址(438. 找到字符串中所有字母异位词)
https://leetcode.cn/problems/find-all-anagrams-in-a-string/
题目描述
给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
示例 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" 的异位词。
提示:
1 <= s.length, p.length <= 3 * 104
s 和 p 仅包含小写字母
前置知识
公司
- 暂无
思路
关键点
代码
- 语言支持:Python3
Python3 Code:
class Solution:
def countChrs(self, w):
res = [0]*26
for c in w:
res[ord(c)-ord('a')] += 1
return res
def findAnagrams(self, s: str, p: str) -> List[int]:
if not s or not p:
return []
res = []
n = len(p)
target = self.countChrs(p)
i = 0
window = [0] * 26
while i < n and i < len(s):
window[ord(s[i]) - ord('a')] += 1
i += 1
i = 0
j = n
while i < len(s) - n + 1:
if target == window:
res.append(i)
if j < len(s):
window[ord(s[j]) - ord('a')] += 1
window[ord(s[i]) - ord('a')] -= 1
i += 1
j += 1
return res
复杂度分析
令 n 为数组长度。
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)