52 regular expression match

Title Description

Implement comprises a function to match '' and '*' regular expression. Mode character '.' Represents any one character '*' indicates that the preceding character can appear any number of times (including 0 time). In this problem, the match is a whole pattern matches all characters of the string. For example, the string "aaa" mode and "aa" and "ab * ac * a" match, but the "aa.a" and "ab * a" does not match

Ideas analysis

  1. The next character is not the mode *
    If the first character to match, the strings and patterns are moved back one,
    if not match, direct return false;
  2. The next character mode is *
    If the first character string and the first character does not match the pattern, after the two-shift mode, continue to match.
    If the first character to match, there are three treatment conditions:
    • After the two-shift mode, the equivalent of x * is ignored.
    • After a string shift mode unchanged and continue to match a string * can more than match.
    • After a string move, the two-shift mode, and skip the next character matches a pattern (corresponding to match a character).
  3. Complete traversal, str not yet reached to the tail end of a given pattern is not matched, if not yet reached str tail-to-tail pattern, must be behind the pattern A B C * format.

If you do not recursion, according to this logic written in pure circulation, "", ". *" , Such as in front of the empty cases pass ... with the compiler can not pass this parameter, test yourself, do not know supposed to.

Code

public static boolean match(char[] str, char[] pattern) {

    if (str == null || pattern == null) {
        return false;
    }
    int strIndex = 0, patternIndex = 0;
    return process(str, strIndex, pattern, patternIndex);
}

private static boolean process(char[] str, int strIndex, char[] pattern, int patternIndex) {
    if (strIndex == str.length && patternIndex == pattern.length) {
        return true;
    }
    //str未到末尾,pattern到末尾
    if (strIndex != str.length && patternIndex == pattern.length) {
        return false;
    }
    //str到尾,pattern未到尾
    if (strIndex == str.length && patternIndex != pattern.length) {
        //只有pattern剩下的部分类似a*b*c*的形式,才匹配成功
        if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
            return process(str, strIndex, pattern, patternIndex + 2);
        }
        return false;
    }
    //str和pattern都未到尾
    //pattern后跟*
    if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
        if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) {
            return process(str, strIndex, pattern, patternIndex + 2)//*匹配0个,跳过
                    || process(str, strIndex + 1, pattern, patternIndex + 2)//*匹配1个,跳过
                    || process(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个
        } else {
            //*匹配0个,直接跳过
            return process(str, strIndex, pattern, patternIndex + 2);
        }
    }
    //pattern后不跟*,两串都++
    if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) {
        return process(str, strIndex + 1, pattern, patternIndex + 1);
    }
    return false;
}
Published 117 original articles · won praise 8 · views 3707

Guess you like

Origin blog.csdn.net/qq_34761012/article/details/104513189