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:

 1 // 1RE, 4WA, 1AC, that was quite a problem... no DP and no recursion is tough.
 2 #include <cstring>
 3 #include <vector>
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     bool isMatch(const char *s, const char *p) {
 9         int i, j;
10         int ls, lp;
11         vector<int> last_i_arr;
12         vector<int> last_j_arr;
13         
14         if (s == nullptr || p == nullptr) {
15             return false;
16         }
17         
18         ls = strlen(s);
19         lp = strlen(p);
20         if (lp == 0) {
21             // empty patterns are regarded as match.
22             return ls == 0;
23         }
24         
25         // validate the pattern string.
26         for (j = 0; j < lp; ++j) {
27             if (p[j] == '*' && (j == 0 || p[j - 1] == '*')) {
28                 // invalid pattern string, can't match.
29                 return false;
30             }
31         }
32         
33         int last_i, last_j;
34         
35         i = j = 0;
36         last_i = -1;
37         last_j = -1;
38         while (i < ls) {
39             if (j + 1 < lp && p[j + 1] == '*') {
40                 last_i_arr.push_back(i);
41                 last_j_arr.push_back(j);
42                 ++last_i;
43                 ++last_j;
44                 j += 2;
45             } else if (p[j] == '.' || s[i] == p[j]) {
46                 ++i;
47                 ++j;
48             } else if (last_j != -1) {
49                 if (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]]) {
50                     // current backtracking position is still available.
51                     i = (++last_i_arr[last_i]);
52                     j = last_j_arr[last_j] + 2;
53                 } else if (last_j > 0) {
54                     while (last_j  >= 0) {
55                         // backtrack to the last backtracking point.
56                         --last_i;
57                         --last_j;
58                         last_i_arr.pop_back();
59                         last_j_arr.pop_back();
60                         if (last_j >= 0 && (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]])) {
61                             i = (++last_i_arr[last_i]);
62                             j = last_j_arr[last_j] + 2;
63                             break;
64                         }
65                     }
66                     if (last_j == -1) {
67                         return false;
68                     }
69                 } else {
70                     // no more backtracking is possible.
71                     return false;
72                 }
73             } else {
74                 return false;
75             }
76         }
77         
78         while (j < lp) {
79             if (j + 1 < lp && p[j + 1] == '*') {
80                 j += 2;
81             } else {
82                 break;
83             }
84         }
85         
86         last_i_arr.clear();
87         last_j_arr.clear();
88         return j == lp;
89     }
90 };

 

 posted on 2014-03-01 21:09  zhuli19901106  阅读(386)  评论(0编辑  收藏  举报