HardPro challengePythonJavaScriptTypeScript
Strings — Wildcard Matching
AlgorithmsStringsGreedyPattern Matching
solve(s, p) — s is a lowercase string and p a pattern made of lowercase letters,? and *. Return true if the pattern matches the entire string, where:
?matches any single character;*matches any sequence, including the empty one.
A DP table is O(n · m) memory. The greedy version needs O(1): walk both strings together;
on a *, remember its position and the current spot in s, and tentatively match it
with nothing. When a later mismatch happens, go back to the last *, let it swallow one more
character of s, and retry. Trailing stars in the pattern can always match the empty string.
Examples
solve("aa", "a") → false
solve("aa", "*") → true
solve("cb", "?a") → false
solve("adceb", "*a*b") → true
solve("acdcb", "a*c?b") → falseSample tests
Test #1Pattern too short
Input: ["aa","a"]
Output: false
Test #2Star matches everything
Input: ["aa","*"]
Output: true
Test #3Question mark then mismatch
Input: ["cb","?a"]
Output: false