> ## 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.

# Wildcard Matching Python Solution with Tests

> Tested Python solution for LeetCode 44 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 44, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy), [Recursion](/catalog/topics/recursion). [View on LeetCode](https://leetcode.com/problems/wildcard-matching/description/).

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

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

## Problem

Given an input string `s` and a pattern `p`, implement wildcard pattern matching with support for `'?'` and `'*'` where:

* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).

The matching should cover the **entire** input string (not partial).

### Examples

```
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
```

```
Input: s = "aa", p = "*"
Output: true
Explanation: '*' matches any sequence.
```

```
Input: s = "cb", p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
```

### Constraints

* 0 \<= s.length, p.length \<= 2000
* `s` contains only lowercase English letters.
* `p` contains only lowercase English letters, `'?'` or `'*'`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(s_len * p_len) worst case (star backtracking), Space: O(1)
    def is_match(self, s: str, p: str) -> bool:
        s_len, p_len = len(s), len(p)
        i = j = 0
        star_idx = -1
        match_idx = 0
        while i < s_len:
            if j < p_len and (p[j] == "?" or p[j] == s[i]):
                i += 1
                j += 1
            elif j < p_len and p[j] == "*":
                star_idx = j
                match_idx = i
                j += 1
            elif star_idx >= 0:
                j = star_idx + 1
                match_idx += 1
                i = match_idx
            else:
                return False
        while j < p_len and p[j] == "*":
            j += 1
        return j == p_len
```

## Complexity

| Time | Space |
| - | - |
| O(s\_len \* p\_len) worst case (star backtracking), Space: O(1) | - |

## Tags


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