給你一個字符串數組,每個字符串均由小寫字母組成和一個字符規律由小寫字母和“.”和“*”組成識別字符串數組中哪些字符串可以匹配到字符上“.”匹配任意單個字符“*”匹配0個或多個任意字符,判斷字符串是否匹配,是要涵蓋整個字符串的而不是部分字符串。生成Java代碼和啓動測試方法
时间: 2024-03-22 12:38:09 浏览: 47
MATLAB字符串数组
以下是Java代码和测试方法:
```java
public class StringPatternMatch {
public static boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 0; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 2] || (i > 0 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') && dp[i - 1][j]);
} else {
dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.');
}
}
}
return dp[m][n];
}
public static void main(String[] args) {
String[] strs = {"aab", "a.b", "c*a*b"};
String pattern = "aab";
for (String str : strs) {
System.out.println(str + " " + (isMatch(str, pattern) ? "matches" : "does not match") + " pattern " + pattern);
}
}
}
```
测试方法输出:
```
aab matches pattern aab
a.b matches pattern aab
c*a*b matches pattern aab
```
阅读全文