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

思路:DP

1.如果p结束的时候,s必须结束,否则false;
2.*(p+1)=='*'的情况:*p为'.'或者*p==*s 循环到s的结尾;
3.*(p+1)不是'*'的情况,逐字检查就好。

AC code:
bool isMatch(char* s, char* p) {
    if(s==NULL||p==NULL)
    return false;
    
    if(*p=='\0') return *s=='\0';
    
    if(*(p+1)=='*')
    {
        while(*s!='\0'&&*p=='.'||*s==*p)
        {
            if(isMatch(s,p+2))
            return true;
            s++;
        }
        
        return isMatch(s,p+2);
    }
    else if((*s!='\0'&&*p=='.')||*s==*p)
    {
        return isMatch(s+1,p+1);
    }
    
    return false;
}

 

相关文章:

  • 2022-01-20
  • 2021-06-05
  • 2022-02-25
  • 2021-07-03
  • 2022-01-14
  • 2022-12-23
  • 2021-12-17
  • 2021-07-25
猜你喜欢
  • 2021-11-24
  • 2021-10-20
  • 2021-06-18
  • 2021-10-04
  • 2021-11-22
  • 2021-10-28
  • 2022-03-06
相关资源
相似解决方案