正则表达式匹配
题目描述
请实现一个函数用来匹配包括‘.‘和‘*‘的正则表达式。模式中的字符‘.‘表示任意一个字符,而‘*‘表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
public class Solution { public boolean match(char[] str, char[] pattern) { int m = str.length, n = pattern.length; boolean [][] dp = new boolean[n + 1][m + 1]; dp[0][0] = true; for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (pattern[i - 1] == ‘*‘) { if (dp[i - 2][j]) dp[i][j] = true; if (j > 0 && (pattern[i - 2] == ‘.‘ || str[j - 1] == pattern[i - 2]) && (dp[i - 2][j - 1] || dp[i][j - 1])) dp[i][j] = true; } else { if (j != 0) { if ((pattern[i - 1] == ‘.‘ || pattern[i - 1] == str[j - 1]) && dp[i - 1][j - 1]) dp[i][j] = true; } } } } return dp[n][m]; } }
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03