oneby7

2-D Dynamic Programming Two strings

564. Wildcard Matching

Hard

You are given a string s and a pattern p. Return true if the pattern matches the whole of s, and false otherwise.

The pattern is made of lowercase letters and two wildcards:

  • A letter matches that same letter.
  • ? matches exactly one character, whatever it is.
  • * matches any run of characters, including the empty run.

The match has to cover s from its first character to its last. Matching only part of s is not enough.

Example 1

Input: s = "sunflower", p = "s*f?ow*"

Output: true

Explanation: The first * matches "un", the ? matches l, and the last * matches "er". The letters s, f, o and w match themselves.

Example 2

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

Output: true

Explanation: Each * matches the empty run, so the pattern matches the empty string.

Example 3

Input: s = "cabin", p = "*b?"

Output: false

Explanation: The pattern's b can only be the b in s. Then the ? matches i, and the n is left over, so the pattern does not cover all of s.

Check your understanding

Work out the answer for this input before you write any code.

s = "pin", p = "p*in*"

Constraints

  • 0 ≤ s.length, p.length ≤ 3000
  • s holds only lowercase English letters.
  • p holds only lowercase English letters, ? and *.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.