【剑指offer】正则表达式匹配(字符串)

版权声明:本文为原创博客,未经允许,请勿转载。 https://blog.csdn.net/u013095333/article/details/88600776

题目描述

请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配

链接

https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

代码

class Solution {
public:
    bool match(char* str, char* pattern)
    {
        if(str == NULL || pattern == NULL){
            return false;
        }
        return matchCore(str, pattern);
    }
    bool matchCore(char* str, char* pattern){
        if(*str == '\0' && *pattern == '\0'){
            return true;
        }
        if(*str != '\0' && *pattern == '\0'){
            return false;
        }
        if(*(pattern+1) == '*'){
            if(*str == *pattern ||(*pattern == '.' && *str != '\0')){
                return matchCore(str+1, pattern) || matchCore(str, pattern+2);
            }
            else{
                return matchCore(str, pattern+2);
            }
        }
        if(*str == *pattern ||(*pattern == '.' && *str != '\0')){
            return matchCore(str+1, pattern+1);
        }
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/u013095333/article/details/88600776