您的位置:首页 > 其它

LeetCode - Regular Expression Matching

2014-03-01 21:09 309 查看
Regular Expression Matching

2014.3.1 20:55

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


Solution:

  This problem has some connection with Wildcard Matching, but the backtracking strategy is a bit different.

  In this problem, my solution is to match the letters and record the '*'s as they appear.

  When a mismatch happened, you keep backtracking until a match is found, while in "Wildcard Matching", you may only backtrack to the last '*'.

  If you have no where else to backtrack and there is not a single match for the current letter, return false.

  Still, you have to ignore the redundant '*'s at the tail of the pattern string if there are any.

  Total time complexity is O(len(s) * len(p)), but it wouldn't appear that large. I guess for more of the test cases it is near O(len(s) + len(p)). Space complexity is O(len(p)), as it is used to record the appearance of '*'s.

Accepted code:

// 1RE, 4WA, 1AC, that was quite a problem... no DP and no recursion is tough.
#include <cstring>
#include <vector>
using namespace std;

class Solution {
public:
bool isMatch(const char *s, const char *p) {
int i, j;
int ls, lp;
vector<int> last_i_arr;
vector<int> last_j_arr;

if (s == nullptr || p == nullptr) {
return false;
}

ls = strlen(s);
lp = strlen(p);
if (lp == 0) {
// empty patterns are regarded as match.
return ls == 0;
}

// validate the pattern string.
for (j = 0; j < lp; ++j) {
if (p[j] == '*' && (j == 0 || p[j - 1] == '*')) {
// invalid pattern string, can't match.
return false;
}
}

int last_i, last_j;

i = j = 0;
last_i = -1;
last_j = -1;
while (i < ls) {
if (j + 1 < lp && p[j + 1] == '*') {
last_i_arr.push_back(i);
last_j_arr.push_back(j);
++last_i;
++last_j;
j += 2;
} else if (p[j] == '.' || s[i] == p[j]) {
++i;
++j;
} else if (last_j != -1) {
if (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]]) {
// current backtracking position is still available.
i = (++last_i_arr[last_i]);
j = last_j_arr[last_j] + 2;
} else if (last_j > 0) {
while (last_j  >= 0) {
// backtrack to the last backtracking point.
--last_i;
--last_j;
last_i_arr.pop_back();
last_j_arr.pop_back();
if (last_j >= 0 && (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]])) {
i = (++last_i_arr[last_i]);
j = last_j_arr[last_j] + 2;
break;
}
}
if (last_j == -1) {
return false;
}
} else {
// no more backtracking is possible.
return false;
}
} else {
return false;
}
}

while (j < lp) {
if (j + 1 < lp && p[j + 1] == '*') {
j += 2;
} else {
break;
}
}

last_i_arr.clear();
last_j_arr.clear();
return j == lp;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: