804. Wildcard Matcher

A file-search box accepts a pattern p built from lowercase letters and two special symbols. A ? stands for exactly one arbitrary character, and a * stands for any run of characters, including an empty run. Every lowercase letter in the pattern matches only itself.

Given a text s, return true if the pattern matches the entire text, and false if it only matches a part of it or does not match at all. Several * symbols may appear in a row.

Aim for O(n*m) time, where n and m are the lengths of s and p, and O(m) extra space.

Example 1

Input:
s = "sunflowerfield", p = "s*fl?w*d"
Output:
true
Explanation:

The pattern "s*fl?w*d" matches the whole text "sunflowerfield".

Example 2

Input:
s = "quokka", p = "q?o*ka"
Output:
true
Explanation:

The pattern "q?o*ka" matches the whole text "quokka".

Example 3

Input:
s = "ribbon", p = "r*b?n?"
Output:
false
Explanation:

The pattern "r*b?n?" does not match the whole text "ribbon".

Constraints

1 ≤ s.length, p.length ≤ 1000

s contains only lowercase letters; p contains lowercase letters, ? and *.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n*m)
Space
O(m)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…