【LeetCode】438. Find All Anagrams in a String 解題報告(Python)
阿新 • • 發佈:2019-02-02
目錄
題目描述
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input:
s: "abab" p: "ab"
Output:
[0, 1, 2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
題目大意
在s中有多少個位置,從這個位置開始和p等長度的子字串中,所包含的字元和p是一樣的。
解題方法
滑動視窗
這個題考的是時間複雜度。如果判斷兩個切片是否是排列組合的話,時間複雜度略高,會超時。
能AC的做法是用了一個滑動視窗,每次進入視窗的字元的個數+1,超出滑動視窗的字元個數-1.
這樣就一遍就搞定了,而且不用每個切片都算是不是一個排列組合。
Counter大法好,判斷兩個字串是否是排列組合直接統計詞頻然後==判斷即可。
注意如果一個詞出現的次數是0,那麼需要從Counter中移除,因為Counter({‘a’: 0, ‘b’: 1}) w不等於Counter({‘b’: 1})。
from collections import Counter
class Solution(object ):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
answer = []
m, n = len(s), len(p)
if m < n:
return answer
pCounter = Counter(p)
sCounter = Counter(s[:n-1])
index = 0
for index in xrange(n - 1, m):
sCounter[s[index]] += 1
if sCounter == pCounter:
answer.append(index - n + 1)
sCounter[s[index - n + 1]] -= 1
if sCounter[s[index - n + 1]] == 0:
del sCounter[s[index - n + 1]]
return answer
雙指標
二刷的時候也是滑動視窗,但是使用的是雙指標的解法,看上去好像沒有上面這個方法更簡單。
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
count = collections.Counter()
M, N = len(s), len(p)
left, right = 0, 0
pcount = collections.Counter(p)
res = []
while right < M:
count[s[right]] += 1
if right - left + 1 == N:
if count == pcount:
res.append(left)
count[s[left]] -= 1
if count[s[left]] == 0:
del count[s[left]]
left += 1
right += 1
return res
日期
2018 年 1 月 27 日
2018 年 11 月 24 日 —— 週六快樂