Find All Anagrams In String

interview_workbook/leetcode/sliding_window /app/src/interview_workbook/leetcode/sliding_window/find_all_anagrams_in_string.py
View Source

Algorithm Notes

Summary: Find All Anagrams In String — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Find All Anagrams In String

TODO: Add problem description
"""

from collections import Counter

from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def solve(self, *args):
        """Return list of start indices of anagrams of p in s."""
        s, p = args
        res = []
        if len(p) > len(s):
            return res
        p_count = Counter(p)
        s_count = Counter()
        l = 0
        for r, ch in enumerate(s):
            s_count[ch] += 1
            if r - l + 1 > len(p):
                s_count[s[l]] -= 1
                if s_count[s[l]] == 0:
                    del s_count[s[l]]
                l += 1
            if s_count == p_count:
                res.append(l)
        return res


def demo():
    """TODO: Implement demo function."""
    pass


register_problem(
    id=438,
    slug="find_all_anagrams_in_string",
    title="Find All Anagrams in a String",
    category=Category.SLIDING_WINDOW,
    difficulty=Difficulty.MEDIUM,
    tags=["string", "sliding_window", "hashmap"],
    url="https://leetcode.com/problems/find-all-anagrams-in-a-string/",
    notes="",
)