Wildcard matching

Dynamic Programming DP on strings Hard
  • This pattern matching problem forms the basis for one of the most essential functions in computer systems: file or text searching
  • The most basic example is wildcard searches in operating systems
  • In Windows, for instance, '?' is used to replace any single character and '*' is used to replace a string of character(s)
  • Besides, Regular Expressions (regex), a common tool used in programming for string pattern searching and manipulation, also rely on this concept
  • Regex is used extensively in programming from simple string manipulation, data validation, data scraping to more complicated natural language processing tasks

Given a string str and a pattern pat, implement a pattern matching function that supports the following special characters:


'?' Matches any single character.

'*' Matches any sequence of characters (including the empty sequence).


The pattern must match the entire string.

Examples:

Input: str = "xaylmz", pat = "x?y*z"

Output: true

Explanation: 

The pattern "x?y*z" matches the string "xaylmz":

- '?' matches 'a'

- '*' matches "lm"

- 'z' matches 'z'

Input: str = "xyza", pat = "x*z"

Output: false

Explanation: 

The pattern "x*z" does not match the string "xyza" because there is an extra 'a' at the end of the string that is not matched by the pattern.

Input: str = "abc", par = "a?c"

Constraints

  • 1 <= length of(str, pattern) <= 200

Hints

  • Define dp[i][j] as True if str[0:i] matches pat[0:j], otherwise False.
  • "If pat[j-1] == str[i-1] or pat[j-1] == '?', dp[i][j]=dp[i−1][j−1] If pat[j-1] == '*', dp[i][j]=dp[i−1][j](use ‘*‘ as a sequence) OR dp[i][j−1](ignore ‘*‘) Otherwise, dp[i][j] = False."

Company Tags

Qualcomm Texas Instruments PwC NVIDIA AMD Oracle Rockstar Games OYO Rooms Target Medtronic Lyft McKinsey & Company Docker Ubisoft JPMorgan Chase Uber ARM Red Hat Robinhood Siemens Healthineers MongoDB Stripe Epic Systems Zomato Intel Google Microsoft Amazon Meta Apple Netflix Adobe