[String recursion] regular expression matching

Title description

Please implement a function to match regular expressions including'.' and'*'. The character'.' in the pattern means any character, and'*' means that the character before it can appear any number of times (including 0 times). In this question, matching 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 does not match neither "aa.a" nor "ab*a"
Example 1
Input
"aaa", "a*a"
returns the value
true


First, you need to clarify the meaning of the title. Matching means that all characters in the string must match the entire pattern string, that is, both strings must be matched to the end. The idea is to recursively traverse the string and the pattern string to determine whether the current string and the pattern string match, which *should be considered together with the previous string, so there are the following situations:

  • The next character in the pattern string is *:
    • The current string and the pattern string do not match, *the function is to offset the characters of the pattern string, that is, 0 times, the string pointer does not move at this time, the pattern string pointer +2, skip `*``
    • If the current pattern string matches the string, or the pattern string is ., it *may match multiple times or 0 times. Therefore, there are two cases. When multiple matches are made, the string pointer is +1 and the pattern string does not move, that is _match(str, s + 1, pattern, p), matches 0 When the string pointer does not move, the pattern string +2 skips *. It is necessary to pay attention to the premise that the string pointer cannot cross the boundary, that is s < str.length(), the string pointer can continue to go down.
  • The next character is not *:
    • If the pattern string and the string match, or the pattern string is ., the string and the pattern string advance together. It is also necessary to note that the string pointer cannot cross the boundary, that is,s < str.length()
    • Otherwise it returns false and the match fails
public class Solution {
    
    
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * @param str     string字符串
     * @param pattern string字符串
     * @return bool布尔型
     */
    public boolean _match(String str, int s, String pattern, int p) {
    
    
        if (s == str.length() && p == pattern.length()) return true;
        if (p == pattern.length()) return false; // 字符串未匹配完,模式串已经完了
        if ((p + 1) < pattern.length() && pattern.charAt(p + 1) == '*') {
    
    
            // 模式串下一个字符是*
            if (s < str.length() && (str.charAt(s) == pattern.charAt(p) || pattern.charAt(p) == '.'))
                return _match(str, s + 1, pattern, p) || _match(str, s, pattern, p + 2);  // *匹配多次 或 匹配一次
            else
                return _match(str, s, pattern, p + 2); // *的作用是将其抵消
        } else {
    
    
            if (s < str.length() && (str.charAt(s) == pattern.charAt(p) || pattern.charAt(p) == '.'))
                return _match(str, s + 1, pattern, p + 1); // 匹配,两指针前进
            else
                return false;
        }
    }

    public boolean match(String str, String pattern) {
    
    
        // write code here
        return _match(str, 0, pattern, 0);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43486780/article/details/114378357