44. Wildcard matching
Title Source: leetcode https://leetcodecn.com/problems/wildcardmatching
subject
Given a string (s) and a character pattern (P), a wildcard matching supporting ‘?’ and ‘*’ is implemented.
'?' can match any single character.
'*' can match any string (including empty string).
Only when two strings match completely is the match successful.
explain:
 S may be empty and contain only lowercase letters from a to Z.
 P may be empty and contain only lowercase letters from a to Z, as well as the characters? And *.
Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: 'a' cannot match the entire string of 'AA'.
Example 2:
Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' can match any string.
Example 3:
Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' can match 'C', but the second 'a' cannot match 'B'.
Example 4:
Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: the first '*' can match the empty string, and the second '*' can match the string "DCE"
Example 5:
Input:
s = "acdcb"
p = "a*c?b"
Output: false
Thinking of solving problems
Idea: dynamic planning
This problem is similar to 10. Regular expression matching. About【10. Regular expression matching】You can read through the following links:
Leetcode 10. Regular expression matching  Python
But there are some differences between the two questions. 【10. Regular expression matching】The character pattern of this questionp
Is not a separate character, for example:.*
The correlation between the two will form a new matching pattern. And in this question,p
The characters of are independent and not associated with the front and back.
First look at the title description, where the character modep
There are the following situations:

az
In this case, only the corresponding lowercase letters are corresponding 
?
Can match any single character (that is, any letter) 
*
To match any string (including empty string, that is, it can match 0 or more characters)
Use of this questiondynamic programmingFirst define the state and use thedp[i][j]
expresss
The first I strings and patterns ofp
Whether the first j strings of match. Then, the following situations will be encountered during state transition:
 If
p[j1] == s[i1]
perhapsp[j1] == '?'
If the current ith character is matched, thendp[i][j]
Can be determined bydp[i1][j1]
It’s transformed. 
If
p[j1] == '*'
Indicates that 0 or more characters can be matched. thatdp[i][j]
Can be determined bydp[i][j1]
perhapsdp[i1][j]
It’s transferred. The details are as follows: When using asterisk matching,
dp[i][j]
Can be determined bydp[i1][j]
Transferred from  When asterisk matching is not used,
dp[i][j]
Can be determined bydp[i][j1]
Transferred from
 When using asterisk matching,
Therefore, the state transfer equation is as follows:
Now start the initialization, but here you need to pay attention to the boundary problem:
 initialization
dp[0][0]=True
, which means that when the string s and the character pattern P are empty, it means that they can be matched; 
dp[i][0]
Indicates that the character pattern P is empty and cannot match a non empty string, which must be false; (initial DP is false) 
dp[0][j]
This means that the character pattern P is not empty and matches an empty string. Here, only the first j characters of the character pattern are asterisks*
Can be set up, so it also needs to be initialized separately.
The specific code implementation is as follows.
code implementation
class Solution:
def isMatch(self, s: str, p: str) > bool:
s_length = len(s)
p_length = len(p)
#Initialize DP
dp = [[False] * (p_length+1) for _ in range(s_length + 1)]
#Initialize DP[0][0]，表示空模式匹配空字符串
dp[0][0] = True
#Initialize DP[0][j]，非空模式匹配空字符串
for j in range(1, p_length+1):
if p[j1] != "*":
break
dp[0][j] = True
for i in range(1, s_length+1):
for j in range(1, p_length+1):
#Substituting transfer equation
if p[j1] == s[i1] or p[j1] == "?":
dp[i][j] = dp[i1][j1]
elif p[j1] == "*":
dp[i][j] = dp[i1][j] or dp[i][j1]
return dp[s_length][p_length]
Implementation results
Article original, welcome to pay attention to praise. The official account of WeChat is updated in parallel.