文章--LeetCode算法--TwoSumRegularExpressionMatching
RegularExpressionMatching
问题描述
Implement regular expression matching with support for '.' and '*'.
实例
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
- isMatch("aa","a") ? false
- isMatch("aa","aa") ? true
- isMatch("aaa","aa") ? false
- isMatch("aa", "a*") ? true
- isMatch("aa", ".*") ? true
- isMatch("ab", ".*") ? true
- isMatch("aab", "cab") ? true
实现代码
public class Solution {
public boolean isMatch(String str, String regex) {
boolean[][] dp = new boolean[str.length() + 1][regex.length() + 1];
dp[0][0] = true;
for (int i = 1; i < regex.length() + 1; i++) {
if (regex.charAt(i - 1) == '*')
dp[0][i] = dp[0][i - 2];
}
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (match(str.charAt(i - 1), regex.charAt(j - 1))) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
if (regex.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 2];
if (match(str.charAt(i - 1), regex.charAt(j - 2))) {
dp[i][j] |= dp[i - 1][j];
}
}
}
}
}
return dp[str.length()][regex.length()];
}
private boolean match(char c1, char r) {
return c1 == r || r == '.';
}
}

浙公网安备 33010602011771号