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