All problems

Wildcard Matching

Hard · dynamic programming

You are given a text s and a pattern p. The pattern is made of three kinds of symbol:

Return true if the pattern matches the whole of s from the first character to the last, and false otherwise. Matching only a piece of s does not count.

Either string may be empty. An empty pattern matches only an empty text.

Examples

Example 1

Input: s = "cabbage", p = "c*b?ge"

Output: true

Read it as c, then * standing for "ab", then b, then ? standing for "a", then g and e.

Example 2

Input: s = "abc", p = "a?"

Output: false

The pattern stops after two characters, but the text has three.

Example 3

Input: s = "", p = "***"

Output: true

Each star can stand for an empty run, so the pattern matches the empty text.

Constraints