通配符匹配 && 正则表达式匹配
作者:
tom233
,
2021-01-28 20:19:21
,
所有人可见
,
阅读 493
LeetCode 10
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size(), m = p.size();
s = ' ' + s , p = ' ' + p;
vector<vector<bool>> dp(n+1 , vector<bool>(m+1));
dp[0][0]=true;
for(int i = 0; i <= n; i ++ )
{
for(int j = 1; j <= m; j ++ )
{
if(j+1 <= m && p[j+1] == '*')continue;
if(p[j] != '*' && i)
dp[i][j] = dp[i-1][j-1] && (p[j] == s[i] || p[j] == '.');
else if(p[j] == '*'){
dp[i][j] = dp[i][j-2] || i && dp[i-1][j] && (p[j-1] == s[i] || p[j-1] == '.');
}
}
}
return dp[n][m];
}
};
LeetCode 44
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size(), m = p.size();
vector<vector<bool>> dp(n + 1, vector<bool>(m + 1));
s = ' ' + s , p = ' ' + p ;
dp[0][0] = true;
for(int i = 0 ; i <= n ; i ++ )
for(int j = 1 ; j <= m ;j ++ )
{
if(p[j] != '*')
dp[i][j] = i && dp[i - 1][j - 1] && (p[j] == s[i] || p[j] == '?');
else
dp[i][j] = i && dp[i - 1][j] || dp[i][j - 1];
}
return dp[n][m];
}
};