zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    最近的3个困惑:信守承诺、技术产品先行还是市场销售先行、客户从哪来
    最近的3个困惑:信守承诺、技术产品先行还是市场销售先行、客户从哪来
    详细回复某个CSDN网友,对我的文章和技术实力以及CSDN的吐槽
    详细回复某个CSDN网友,对我的文章和技术实力以及CSDN的吐槽
    2015年工作中遇到的问题:21-30(这10个问题很有价值)
    使用ABAP(ADBC)和Java(JDBC)连接SAP HANA数据库
    C4C和Outlook的集成
    Hybris开发环境的license计算实现
    CRM WebClient UI和Hybris里工作中心跳转的url生成逻辑
    CRM WebUI and Hybris的Product页面标题实现
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3575644.html
Copyright © 2011-2022 走看看