LeetCode 438 Find All Anagrams in a String (Python)

Posted by 小明MaxMing on May 17, 2020

题目

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.

解题思路

用sliding window扫描s,把字符串放到一个计数器中,每次更新这个计数器并和p的计数器比较,如果相同就是一个答案

代码

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        def f(c):
            return ord(c) - 97
    
        ct_p, ct_s = [0] * 26, [0] * 26
        for c in p:
            ct_p[f(c)] += 1
        l = len(p)
        for c in s[:l - 1]:
            ct_s[f(c)] += 1
        res = []
        for i, c in enumerate(s[l - 1:]):
            ct_s[f(c)] += 1
            if ct_s == ct_p:
                res.append(i)
            ct_s[f(s[i])] -= 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅