File: number-of-strings-that-appear-as-substrings-in-word/solution.py

Date: 2026-06-06

Time: 18:22

Purpose

This file solves LeetCode 1967: Number of Strings That Appear as Substrings in Word. It owns exactly one responsibility: given a list of pattern strings and a target word, count how many patterns appear as substrings of that word.

Within the project, it follows the standard convention — each problem directory contains a solution.py with a Solution class that LeetCode's judge can invoke.

Key Components

Solution.numOfStrings(patterns, word) -> int — The sole method. Takes a list of strings and a target string, returns the count of patterns found as substrings in word. The contract matches LeetCode's expected signature: List[str] input, int output.

Patterns

The implementation uses a generator expression with sum — a standard Python idiom for counting elements that satisfy a predicate. sum(1 for p in patterns if p in word) is functionally equivalent to len([p for p in patterns if p in word]) but avoids allocating an intermediate list.

The substring check itself delegates entirely to Python's in operator on strings, which under the hood uses a fast substring search (a variant of Boyer-Moore/Horspool in CPython).

Dependencies

Imports: typing.List — used only for the type annotation. In Python 3.9+ this could be replaced with list[str], but the project uses the typing import consistently across all solutions for compatibility.

Imported by: The test_solution.py in the same directory imports this Solution class. The massive "Imported By" list in the prompt is misleading — those are test files from *other* problem directories that happen to share the same import pattern (from solution import Solution), not actual cross-problem dependencies.

Flow

1. Iterate over each string p in patterns

2. For each p, test p in word — Python's string containment check

3. Yield 1 for every match

4. sum accumulates the count

No intermediate data structures, no early termination. Every pattern is checked unconditionally.

Invariants

Error Handling

None. The method trusts its inputs conform to LeetCode's constraints (non-null list, non-null strings). This is appropriate — validation happens at the platform boundary, not inside the solution.

Topics to Explore

Beliefs