Niuke.com's sword refers to Offer - regular expression matching

Topic description

Please implement a function to match regular expressions including '.' and '*'. The character '.' in the pattern means any character, and '*' means that the character preceding it can appear any number of times (including 0). In this question, match means that all characters of the string match the entire pattern. For example, the string "aaa" matches the patterns "aa" and "ab*ac*a", but neither "aa.a" nor "ab*a".

The input string is str and the pattern is pattern

Problem solving ideas

Solve problems using recursive methods.

1. When the second character in the pattern is not "*", it is simpler:

(1) If the first character of the string matches the first character in the pattern, that is, *str=*pattern or *str=' \0 ' && *pattern!=' . ', then both the string and the pattern are Shift one character back, i.e. str=str+1, pattern=pattern+1 and match the rest.
(2) If the first character of the string does not match the first character in the pattern, return false directly.

2. The next character in the pattern string is *, that is, *(pattern+1)=='*', there are two situations:
(1) If the current character matches, that is, *str=*pattern or *str='.' && *pattern!='\0', three possibilities:
    ① The current character of the pattern string appears 0 times, that is, * means that the current character appears 0 times, then str=str, pattern=pattern+2; (for example, str=ac, pattern= ab*c, the b* part should appear 0 times, skip b* directly with pattern+2);
    ② The current character of the pattern string appears once, that is, * means the current character appears once, then str=str+1,pattern= pattern+2; (for example, str=abc, pattern=ab*c, b should appear once, so the next time str+1, pattern+2);
    ③ The current character of the pattern string appears twice or more, that is, * means the current If the character appears 2 times or more, then str=str+1, pattern=pattern; (for example: str=abbc, pattern=ab*c, b needs to appear more than once, so str+1, and pattern remains unchanged);
(2 ) If the current character does not match, you can only let * indicate that the current character appears 0 times, then str=str, pattern=pattern+2;

code

class Solution {
public:
    bool match(char* str, char* pattern)
    {
        if( str == nullptr && pattern == nullptr )
            return true;
        if( str == nullptr || pattern == nullptr )
            return false;
        return help(str, pattern);
    }
    
    bool help( char* str, char* pattern )
    {
        if( *str == '\0' && *pattern == '\0' )
            return true;
        if( *pattern == '\0' )
            return false;
        
        if( *(pattern+1) == '*' )
        {
            if( *str == *pattern || (*pattern == '.' && *str != '\0') )
                return help(str, pattern+2) || help(str+1, pattern+2) || help(str+1, pattern);
            else
                return help(str, pattern+2);
        }
        
        if( *str == *pattern || (*pattern == '.' && *str != '\0') ) // must be written after the previous case because *(pattern+1) == ' *'In this case, there are also cases where *str == *pattern || (*pattern == '.' && *str != '\0'), if this sentence is written in front, there may be a pattern If the next character of pattern is *, if you want to write it in front, you need to exclude the case where the next character of pattern is *
            return help(str+1, pattern+1);
        
        return false;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325628445&siteId=291194637