> For the complete documentation index, see [llms.txt](https://llssff.gitbook.io/coding-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://llssff.gitbook.io/coding-problems/dynamic-programming/wild-card-matching.md).

# Wild card matching

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

**Example 1:**

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

**Example 2:**

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

**Example 3:**

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

**Example 4:**

```
Input: s = "adceb", p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".
```

### Explanation

#### State Transition

* Case 1: If the current pattern character matches the current input character or is a '?", mark the current cell as *true*.
* Case 2: If the current pattern character is a '\*', mark the current cell as *true* **if**
  * \[null match] the current input character can be matched by the previous pattern character
    * M\[i]\[j] = M\[i]\[j-1]
  * OR
  * \[1+ match] the current character can be matched by the '\*' if last input char is\_match
    * can be thought of as "skipping" over the current input character
    * M\[i]\[j] = M\[i-1]\[j]
* Case 3: No match

**State Definition**

* i (int)
  * the index up to which the **input** is matched
* j (int)
  * the index up to which the **pattern** is matched
* is\_match (bool):&#x20;
  * True if for the given i, j the pattern+input substrings are a **match**

**State Initialization (base case)**

* i=0, j=0 is **true**
  * empty pattern matches the empty string
* i>0, j=0 is **false**
  * empty pattern cannot match non-empty string
* i=0, j>0 is **true if** the current pattern character is '\*' and the previous is also a match
  * can be thought of as a 'leading asterisk' case
  * any leading characters which can match an empty string are true. Otherwise false

```go
func Match(pattern string, input string) bool {
  // init memo matrix
  M := make([][]bool, len(input)+1)

  for i := 0; i <= len(input); i++ {
    M[i] = make([]bool, len(pattern)+1)
  }

  // base case
  // empty pattern matches empty string
  M[0][0] = true
  
  for i := 1; i <= len(input); i++ {
    // cannot match empty pattern against non-empty string
    M[i][0] = false
  }

  for j := 1; j <= len(pattern); j++ {
    // asterisks match empty string
    // iff they are leading
    if pattern[j-1] == '*' {
      M[0][j] = M[0][j-1]
    } else {
      M[0][j] = false // redundant
    }
  }

  //case 1: p[j - 1] == a[i - 1] || p[j - 1] == '?' . Inherit from diagonal
  //case 2: p[j - 1] == '*' 
  //case 2a: Match nothing, M[i][j] = M[i][j - 1]. Matching nothing against prev input char
  //case 2b: Match one or more, M[i][j] = M[i - 1][j]. Matching wildcard to cur input char
  //case 3: p[j - 1] != a[i - 1] False. Can not match no matter what

  for i := 1; i <= len(input); i++ {
    for j := 1; j <= len(pattern); j++ {
      if pattern[j - 1] == input[i - 1] || pattern[j - 1] == '?' {
        M[i][j] = M[i - 1][j - 1]
      } else if pattern[j - 1] == '*' {
        M[i][j] = M[i][j - 1] || M[i - 1][j]
      } else {
        M[i][j] = false
      }
    }  
  }
  return M[len(input)][len(pattern)]
}
```

TC: O(N^2)

SC: O(N^2) can be lowered to O(2N) as only two rows are relevant


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://llssff.gitbook.io/coding-problems/dynamic-programming/wild-card-matching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
