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", "c*a*b") → true
思路:
'*'代表它之前的字符0个或多个匹配。例如"a*"可以匹配“aaaa” 也可以匹配“"一个空字符创。
1) 对于regex index j, 看看p.charAt(j+1)是否为’*‘,如果是的话,我们要对各种情况进行匹配:
a) 目标字符不等于regex当前字符:s[i, length), p[j+2, length)
b) 目标字符等于regex当前字符:s[i+1, length), p[j+2, length)
c) 目标字符多个字符都和regex当前字符相等:s[i+k, length), p[j+2, length) (其中k代表了循环了k次之后目标字符和regex第一个字符还是相等的
b)c)两种情况可以合并。为了效率,先做b,c,尽快缩短目标字符的长度
2) 如果p.charAt(j+1) != '*',就看看regex, 目标字符的第一个是否相等或者regex= ’.'。这里一个corner case是要注意目标字符是否已经到了最后一个位置。
3) 如果regex已经扫到了最后,就要看目标字符也是否到最后,因为题目要求是“The matching should cover the entire input string (not partial)”
2015.3 update 用自己的思路重新写了一遍,更容易理解
public boolean isMatch(String s, String p) {
if (p.length() == 0) {
return s.length() == 0;
}
if (s.length() == 0) {
if (p.length() >= 2 && p.charAt(1) == '*') {
return isMatch(s, p.substring(2));
} else {
return false;
}
}
if (p.length() >= 2 && p.charAt(1) == '*') {
if (isMatch(s, p.substring(2))) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (matchChar(s.charAt(i), p.charAt(0))) {
if (isMatch(s.substring(i+1), p.substring(2))) {
return true;
}
} else {
break;
}
}
} else {
if (matchChar(s.charAt(0), p.charAt(0))) {
return isMatch(s.substring(1), p.substring(1));
}
}
return false;
}
boolean matchChar(char a, char b) {
if (b == '.' || a == b) {
return true;
}
return false;
}
dp: 思路完全一样
if (s[i] match p[j]) d[i][j] = d[i+1][j+1]
if (p[i+1] == '*') d[i][j] = d[k+1][j+2] k = i-1.....m-1, given that either k == i-1 or s[k] match p[j]
还有一个容易忘记的问题就是初始化p的结尾是”*x“这种情况
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] d = new boolean[m+1][n+1];
d[m][n] = true;
for (int j = n-2; j >= 0; j -= 2) {
if (p.charAt(j+1) == '*') {
d[m][j] = true;
} else {
break;
}
}
for (int i = m-1; i >= 0; i--) {
for (int j = n-1; j >= 0; j--) {
if (p.charAt(j) == '*') {
continue;
}
if (j < n-1 && p.charAt(j+1) == '*') {
for (int k = i-1; k < m; k++) {
if (k == i-1 || s.charAt(k) == p.charAt(j) || p.charAt(j) == '.') {
if (d[k+1][j+2]) {
d[i][j] = true;
break;
}
} else {
break;
}
}
} else if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
d[i][j] = d[i+1][j+1];
}
}
}
return d[0][0];
}
递归:
public class Solution {
public boolean isMatch(String s, String p) {
return helper(s, p, 0, 0);
}
// pos1 s start, pos2 p start
boolean helper(String s, String p, int pos1, int pos2) {
if (pos2 == p.length()) {
return pos1 == s.length(); // if s hasn't finished, return false
}
if (pos2 == p.length()-1 || p.charAt(pos2+1) != '*') { // if current elements match
if (pos1 == s.length() || (p.charAt(pos2) != s.charAt(pos1) && p.charAt(pos2) != '.')) {
return false;
} else {
return helper(s, p, pos1+1, pos2+1);
}
}
// p.charAt(pos2+2) == '*'
while (pos1 < s.length() && (p.charAt(pos2) == '.' || p.charAt(pos2) == s.charAt(pos1))) {
if (helper(s, p, pos1, pos2+2)) {
return true;
}
pos1++;
}
return helper(s, p, pos1, pos2+2); <span style="color:#ff0000;">// if judge this condition first, time exceeds limit. we want to shorten s asap</span>
}
}