Sword means regular expression matching

topic link

First, consider the special case:
         1> Both strings are empty, return true
         2> When the first string is not empty, and the second string is empty, return false (because in this way, the
            match cannot be successful, And if the first string is empty and the second string is not empty, it may still match
            successfully . For example, the second string is "a*a*a*a*", because the element before '*' It can appear 0 times,
            so it is possible to match successfully)
    and then start to match the first character. There are two possibilities: the match succeeds or the match fails. But considering that the
    next character of pattern may be '*', here we discuss it in two cases: the next character of pattern is '*' or
    not '*':
          1> The next character of pattern is not '*': this The situation is relatively simple, directly matching the current character. If
            the match is successful, continue to match the next; if the match fails, return false directly. Note that
            "matching is successful" here, in addition to the case where the two characters are the same, there is another case, that is, the
            current character of pattern is '.', and the current character of str is not '\0'.
          2> When the next character of pattern is '*', it is a little more complicated, because '*' can represent 0 or more.
            All these situations are taken into account here:
               a> When '*' matches 0 characters, the current character of str remains unchanged, and the current character of pattern is shifted by two digits,
                Skip this '*' symbol;
               b> When '*' matches 1 or more, the current character of str moves to the next one, and the current character of pattern
                remains unchanged. (One or more matches here can be regarded as a situation, because: when matching one,
                since str moves to the next character, and the pattern character remains unchanged, it returns to the above case a;
                when more than one character is matched , which is equivalent to continuing to match from the next character of str)
    and then writing the code is very simple.

 

class Solution {
public:
    bool match(char* str, char* pattern)
    {
        if (*str == '\0' && *pattern == '\0')
            return true;
        if (*str != '\0' && *pattern == '\0')
            return false;
        //if the next character in pattern is not '*'
        if (*(pattern+1) != '*')
        {
            if (*str == *pattern || (*str != '\0' && *pattern == '.'))
                return match(str+1, pattern+1);
            else
                return false;
        }
        //if the next character is '*'
        else
        {
            if (*str == *pattern || (*str != '\0' && *pattern == '.'))
                return match(str, pattern+2) || match(str+1, pattern);
            else
                return match(str, pattern+2);
        }
    }
};

 

Guess you like

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