class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @param pattern string字符串 * @return bool布尔型 */ bool match(string str, string pattern) { int m = str.length(); int n = pattern.length(); vector< vector<int> > dp(m + 1, vector<int>(n + 1, 0)); dp[0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 1; j <= n; j++) { if (i > 0 && str[i - 1] == pattern[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { if (pattern[j-1] == '.') { if(i>0) dp[i][j] = dp[i - 1][j - 1]; } if (pattern[j-1] == '*') { if(j>=2) dp[i][j]=dp[i][j]||dp[i][j-2]; if(i>=1&&j>=2&&(str[i-1]==pattern[j-2]||pattern[j-2]=='.')) dp[i][j]=dp[i][j]||dp[i-1][j]; } } } } return dp[m][n]; } };