> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Matching Subsequences

> Tested Python solution for LeetCode 792 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 792, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming), [Trie](/catalog/topics/trie), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/number-of-matching-subsequences/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 792   # by problem number
lcpy gen -s number_of_matching_subsequences   # by problem name
```

## Problem

Given a string `s` and an array of strings `words`, return *the number of* `words[i]` *that is a subsequence of* `s`.

A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not).

### Examples

```
Input: s = "abcde", words = ["a","bb","acd","ace"]
Output: 3
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
```

```
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
Output: 2
```

### Constraints

* 1 \<= s.length \<= 5 \* 10^4
* 1 \<= words.length \<= 5000
* 1 \<= words\[i].length \<= 50
* s and words\[i] consist of only lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_matching_subsequences/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_matching_subsequences/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import defaultdict


class Solution:
    # Time: O(len(s) + total length of all words)
    # Space: O(len(words))

    def num_matching_subseq(self, s: str, words: list[str]) -> int:
        # Bucket words by the next character each one is waiting for.
        waiting: dict[str, list[tuple[int, int]]] = defaultdict(list)
        for idx, word in enumerate(words):
            waiting[word[0]].append((idx, 0))

        matched = 0
        for ch in s:
            for idx, pos in waiting.pop(ch, ()):
                nxt = pos + 1
                if nxt == len(words[idx]):
                    matched += 1
                else:
                    waiting[words[idx][nxt]].append((idx, nxt))
        return matched
```

## Complexity

| Time | Space |
| - | - |
| O(len(s) + total length of all words) | O(len(words)) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.