Loading content...
Given a text string s and a pattern string p, implement a glob-style pattern matching algorithm that supports two special metacharacters:
• ? — Matches exactly one arbitrary character (any single character from the text).
• * — Matches any sequence of zero or more characters (including an empty sequence).
The pattern must match the entire input text string from the first character to the last. Partial matches are not valid—if the pattern matches only a substring of the text, return false.
Your task is to determine whether the given pattern p completely matches the text string s.
Understanding the Metacharacters:
The ? metacharacter is straightforward: it consumes exactly one character from the input text, regardless of what that character is. For example, pattern a?c matches abc, axc, a1c, etc.
The * metacharacter is more flexible: it can match zero characters (acting as if it's not there), one character, or many consecutive characters. For example, pattern a*c matches ac (zero characters matched by *), abc (one character), abxyzc (multiple characters), etc.
The key challenge lies in handling the * metacharacter efficiently, as it introduces non-determinism—you may need to explore multiple ways to "consume" characters from the text.
s = "aa"
p = "a"falseThe pattern "a" only matches a single character 'a', but the text "aa" has two characters. Since the pattern does not cover the entire string, the result is false.
s = "aa"
p = "*"trueThe '*' metacharacter can match any sequence of characters, including the entire string "aa". It expands to match both characters, so the pattern covers the full text.
s = "cb"
p = "?a"falseThe '?' metacharacter matches 'c' (any single character), but then the pattern requires 'a' as the second character. However, the text has 'b' as the second character, which does not match 'a'. Therefore, the result is false.
Constraints