101 Logo
onenoughtone

Problem Statement

Wildcard Pattern Matcher

You are developing a file search utility for a cloud storage system. Users need to be able to search for files using wildcard patterns, where certain characters in the pattern can match any character or sequence of characters in the file name.

Your task is to implement a wildcard pattern matcher that supports two special characters:

  • ? (question mark) - Matches any single character
  • * (asterisk) - Matches any sequence of characters (including an empty sequence)

For example, the pattern "file?.txt" would match "file1.txt" and "fileA.txt", but not "file12.txt". The pattern "*.jpg" would match any file with a .jpg extension.

Write a function that takes a text string (representing a file name) and a pattern string, and returns true if the text matches the pattern, and false otherwise. The matching should cover the entire text string.

Examples

Example 1:

Input: text = "report.pdf", pattern = "*.pdf"
Output: true
Explanation: The '*' matches 'report', so the pattern matches the entire text.

Example 2:

Input: text = "document2023.docx", pattern = "document????.docx"
Output: true
Explanation: The four '?' characters match '2023', so the pattern matches the entire text.

Example 3:

Input: text = "image.png", pattern = "*.jpg"
Output: false
Explanation: The pattern cannot match the text because the extensions are different.

Constraints

  • The text and pattern strings consist only of lowercase English letters and the special characters '?' and '*'.
  • The length of the text string is between 0 and 100 characters.
  • The length of the pattern string is between 0 and 100 characters.

Problem Breakdown

To solve this problem, we need to:

  1. The '?' character is straightforward as it matches exactly one character.
  2. The '*' character is more complex as it can match any sequence, including an empty one.
  3. A greedy approach for '*' might not work for all cases, so we need to consider backtracking or dynamic programming.
  4. The problem can be broken down into smaller subproblems by matching one character at a time.
ProblemSolutionCode
101 Logo
onenoughtone