# 44.wildcard-matching

## Statement

• Difficulty: Hard
• Tag: `贪心` `递归` `字符串` `动态规划`

``````'?' 可以匹配任何单个字符。
'*' 可以匹配任意字符串（包括空字符串）。
``````

• `s` 可能为空，且只包含从 `a-z` 的小写字母。
• `p` 可能为空，且只包含从 `a-z` 的小写字母，以及字符 `?` 和 `*`

``````输入:
s = "aa"
p = "a"

``````输入:
s = "aa"
p = "*"

``````

``````输入:
s = "cb"
p = "?a"

``````

``````输入:
p = "*a*b"

``````

``````输入:
s = "acdcb"
p = "a*c?b"

• Link: Wildcard Matching
• Difficulty: Hard
• Tag: `Greedy` `Recursion` `String` `Dynamic Programming`

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'.
``````

Constraints:

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

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
bool isMatch(string s, string p) {
int n = s.length();
int m = p.length();
auto f = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
f[0][0] = 1;
for (int j = 1; j <= m; j++) {
if (p[j - 1] != '*') {
break;
}

f[0][j] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c_s = s[i - 1];
char c_p = p[j - 1];
if (c_p == '?' || c_p == '*' || c_s == c_p) {
f[i][j] |= f[i - 1][j - 1];
}

if (c_p == '*') {
f[i][j] |= f[i - 1][j] | f[i][j - 1];
}
}
}
return f[n][m];
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````
``````/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function (s, p) {
p = p.replace(/\?/g, ".");
p = p.replace(/\*+/g, ".*");
const r = new RegExp(`^\${p}\$`);
return r.test(s);
};
``````