Wildcard Matching
Match an entire string against a pattern where ? matches one character and * matches any sequence.
Intuition
A star can absorb many different substring lengths, so blindly choosing its length can require extensive backtracking. The greedy insight is that only the most recent star needs reconsideration: ordinary characters and ? must match in place. Remember where that star occurred and how many string characters it currently covers. On a mismatch, expand its coverage by one and retry the pattern suffix.
Wildcard matching differs from regex: stands alone and matches arbitrary characters. With only ? and , a linear greedy scan can use the latest star as a controlled backtracking checkpoint.
Approach
Advance through forced single-character matches
When pattern and string characters agree, or the pattern has ?, advance both pointers. These matches have no useful alternative to revisit unless an earlier star exists.
Record the latest star as a retry point
When the pattern pointer sees *, save its index and the current string index, then advance past the star. Initially the star represents an empty sequence.
Expand the saved star after a mismatch
If matching fails and a star was seen, increase the star's matched string endpoint by one, reset the string pointer there, and resume immediately after the star. Once the string ends, skip remaining stars and require the pattern to end.
Solution
Common pitfalls
Giving wildcard star regex semantics
star repeats p[j - 1]
star matches any sequence of characters
In this problem * is an independent wildcard, not a postfix operator.
Retrying from the star itself
j = star
j = star + 1
The saved star already absorbs the expanded substring; matching must resume after it.
Rejecting leftover stars
return j == len(p)
while j < len(p) and p[j] == '*':
j += 1
return j == len(p)A trailing star may match the empty sequence.
Edge cases
The final cleanup skips every star and accepts.
Each is recorded and skipped; their combined behavior is equivalent to one star.
There is no flexible token to adjust, so return false immediately.