leetcode Wildcard Matching greedy algrithm

The recursive program will result in TLE like this:

 

class Solution {
 public:
  bool isMatch(const char *s, const char *p) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    if (*s == *p && *s == '\0')
      return true;
    if (*p == '?' || *s == *p)
      return isMatch(s + 1, p + 1);
    else if (*p == '*') {
      int i;
      for (i = 0; *(s + i); ++i)
        if (isMatch(s + i, p + 1))
          return true;
      if (isMatch(s + i, p + 1))
        return true;
      
      return false;
    }
    else if (*p != *s)
      return false;
  }
};


So it's necessary to write an non-recursive program. The key point is to match the '*' in p string. We could attempt to match '*' with 0...n characters in s, i.e., the character after '*' maybe match any position in s regardless a series of characters in s. Take notice that consecutive '*'s are equal to one '*'. Based on that, a lengthy code is written as :

 

 

class Solution {
 public:
  bool isMatch(const char *s, const char *p) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    bool star = false, staremerge = false;
    const char *str = s, *ptr = p, *ss = s, *pp = p;
    for (str = ss, ptr = pp; *str && *ptr || *str == '\0' && *ptr == '*'; ++str, ++ptr) {
      if (*ptr == '*') {
        star = staremerge = true;
        while (*ptr == '*')
          ++ptr;
        if (*ptr == '\0')
          return true;
        ss = str;
        pp = ptr;
        --str;
        --ptr;
      }  
      else {
        if (!star) {
          if( !staremerge ) {
            if (*str != *ptr && *ptr != '?' ||*(ptr + 1) == '\0' && *(str + 1) != '\0')  
              return false;
          }
          else {
            if (*str != *ptr && *ptr != '?' ||*(ptr + 1) == '\0' && *(str + 1) != '\0') {
              str = ss++;
              ptr = pp - 1;
              star = true;
            }
              
          }
        }
        else if (star) {
          if ( *str != *ptr && *ptr != '?') {
            ss = str + 1;  
            --ptr;
          }
          else {
            star = false;
            if (*(ptr + 1) == '\0' && *(str + 1) != '\0') {
              str = ss++;
              ptr = pp - 1;
              star = true;
            }            
          }
        }
      }
    }
    if (*str == *ptr && *str == '\0')
      return true;
    else
      return false;
  }
};


Some suggestions about this code: 

 

1. There is no need to refresh the status of star, staremerge. Only one star is enough, because the character(for example, 'a') always needs to match some 'a' in s. Matching the former 'a' is better than the latter 'a' in s as is illustrated in the figure. I.e., there is no need to record the matching range for every '*', the latest '*' has the largest range of choice. 


2. sbegin is refreshed when mismatch occurs and pbegin is refreshed when meeting new '*';

3. Focusing on s is better than handling the two strings at the same time. 


So the final concise code is like:


 

class Solution {
 public:
  bool isMatch(const char *s, const char *p) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    const char *sbegin = s, *pbegin = p, *str = s, *ptr = p;
    bool star = false;
    for (str = s, ptr = p; *str || *ptr == '*'; ++str, ++ptr) {
      if (*ptr == '*') {
        star = true;
        while (*ptr == '*') 
          ++ptr;
        if (*ptr == '\0')
          return true;
        pbegin = ptr--;
        sbegin = str--;
      }
      else if (*str != *ptr && *ptr != '?'){
        if (!star)
          return false;
        str = sbegin++;
        ptr = pbegin - 1;
      }
    }
    return *ptr == '\0';  
  }
};


 


 

转载于:https://www.cnblogs.com/james1207/p/3395436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值