Hone logo
Hone
Problems

Wildcard Pattern Matching

This challenge involves implementing a function that determines if a given string matches a pattern containing wildcard characters. This is a fundamental problem in text processing and pattern recognition, with applications in file searching, regular expression engines, and data validation. You will need to handle two specific wildcard characters: '?' which matches any single character, and '*' which matches any sequence of characters (including an empty sequence).

Problem Description

You are tasked with creating a function isMatch(text, pattern) that returns true if the pattern completely matches the text, and false otherwise.

Key Requirements:

  • The pattern can contain lowercase English letters, '?', and '*'.
  • The text can contain lowercase English letters.
  • '?' matches any single character.
  • '*' matches any sequence of characters (including the empty sequence).
  • The match must cover the entire input string (not partial).

Expected Behavior:

The function should return a boolean value.

Edge Cases to Consider:

  • Empty text and empty pattern.
  • Empty text with a pattern containing only '*'.
  • Non-empty text with an empty pattern.
  • Patterns with consecutive '*' characters.
  • Patterns starting or ending with '*'.

Examples

Example 1:

Input: text = "aa", pattern = "a"
Output: false
Explanation: The pattern "a" only matches the first character of the text "aa". The entire text must be matched.

Example 2:

Input: text = "aa", pattern = "aa"
Output: true
Explanation: Both the pattern and text are the same.

Example 3:

Input: text = "aa", pattern = "a*"
Output: true
Explanation: The pattern "a*" matches the text "aa". The 'a' in the pattern matches the first 'a' in the text, and the '*' matches the second 'a'.

Example 4:

Input: text = "ab", pattern = "?*"
Output: true
Explanation: The pattern "?*" matches the text "ab". The '?' matches 'a', and the '*' matches 'b'.

Example 5:

Input: text = "aab", pattern = "c*a*b"
Output: false
Explanation: The pattern starts with 'c', which does not match the first character 'a' of the text.

Example 6:

Input: text = "acdcb", pattern = "a*c?b"
Output: false
Explanation: The pattern "a*c?b" does not match the text "acdcb". The '*' would match "cd", but then '?' needs to match 'c' which is not possible as '*' can match zero or more characters but not change the characters already matched.

Constraints

  • The length of text is between 0 and 1000, inclusive.
  • The length of pattern is between 0 and 1000, inclusive.
  • text consists only of lowercase English letters.
  • pattern consists only of lowercase English letters, '?', and '*'.
  • Your solution should aim for an efficient time complexity, ideally better than exponential.

Notes

  • Consider how to handle the '*' character. It can match zero characters, one character, or multiple characters. This might suggest a recursive or dynamic programming approach.
  • Think about how to manage the pointers or indices for both the text and the pattern simultaneously.
  • The problem requires an exact match of the entire text.
Loading editor...
plaintext