Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?’ and ‘*’.
‘?’ Matches any single character.
‘*’ Matches any sequence of characters (including the empty sequence).
s = “adceb”
p = “*a*b”
Output: true
Explanation: The first ‘’ matches the empty sequence, while the second ‘’ matches the substring “dce”.
44. Wildcard Matching
解法1:
首先这道题很容易想到用2d dp的思路去做;
虽然复杂度是n*m,相当于是对于每一个(0,i)的目标字串检查是否可以和(0,j)的pattern匹配。
解法2:
我们需要知道每次遇到‘*’的时候我们并不知道此时两个指针应该怎么移动才可以是最优解,所以这里保存开始尝试的位置,如果向后尝试失败,那么我们回溯到当前位置,将p1指针后移一个(相当于星号多匹配一个字符);