【leetcode hot 100】【6】10. Regular expression matching

topic

Given a string s and a character pattern p, please implement a regular expression matching that supports '.' and '*'.

'.' Match any single character
'*' Match zero or more of the previous element The
so-called match is to cover the entire string s, not part of the string.

Example 1:

Input: s = "aa", p = "a"
Output: false
Explanation: "a" cannot match the entire string of "aa".

Example 2:

Input: s = "aa", p = "a*"
Output: true
Explanation: Because '*' means that zero or more of the previous element can be matched, and the previous element here is 'a'. Therefore, the string "aa" can be considered as 'a' repeated once.

Example 3:

Input: s = "ab", p = ".*"
Output: true
Explanation: ". " means that zero or more (' ') arbitrary characters ('.') can be matched.

hint:

  • 1 <= s.length <= 20
  • 1 <= p.length <= 20
  • s contains only lowercase letters from az.
  • p contains only lowercase letters from az, and the characters . and *.
  • Ensure that every time the character * appears, a valid character is matched in front of it

problem solving ideas

This problem can be solved using a dynamic programming algorithm. Specifically, a two-dimensional Boolean array dp can be defined, where dp[i][j] indicates whether the first i characters in the string s match the first j characters in the pattern string p. During state transition, the following situations need to be considered:

  1. If p[j] is a letter, you need to judge whether s[i] and p[j] are equal, if they are equal, then dp[i][j]=dp[i-1][j-1], otherwise dp[i][j]=false.

  2. If p[j] is a point, any character can be matched, so dp[i][j]=dp[i-1][j-1].

  3. If p[j] is an asterisk, zero or more of the preceding element can be matched. Therefore, two situations need to be considered:

    (1) If p[j-1] does not match s[i], then the two characters p[j-1] and p[j] can only match zero times, at this time there is dp[i][j] =dp[i][j-2].

    (2) If p[j-1] matches s[i], then the two characters p[j-1] and p[j] can be matched multiple times or zero times. At this time, dp[i][j ]=dp[i][j-2] (zero matches) or dp[i-1][j] (multiple matches).

Finally, if dp[s.size()][p.size()] is true, it means that the entire string matches successfully, otherwise the match fails.

the code

c++

class Solution {
    
    
public:
    bool isMatch(string s, string p) {
    
    
        int n = s.size(), m = p.size();
        vector<vector<bool>> dp(n+1,vector<bool>(m+1,false));
        dp[0][0] = true; // 当s和p都为空时,匹配成功
        for(int i=0;i<=n;i++){
    
    
            for(int j=1;j<=m;j++){
    
    
                if(p[j-1] == '*'){
    
     // 如果p[j]是星号
                    dp[i][j] = dp[i][j-2]; // 如果星号匹配零个前面的元素,则有dp[i][j]=dp[i][j-2]
                    if(i>0 && (p[j-2] == s[i-1] || p[j-2] == '.')){
    
     // 如果星号匹配多个前面的元素
                        dp[i][j] = dp[i][j] || dp[i-1][j]; // 此时有dp[i][j]=dp[i][j-2]或dp[i-1][j]
                    }
                }
                else if(i>0 && (s[i-1] == p[j-1] || p[j-1] == '.')){
    
     // 如果p[j]是字母或点
                    dp[i][j] = dp[i-1][j-1]; // 如果s[i]和p[j]相等,则有dp[i][j]=dp[i-1][j-1]
                }
            }
        }
        return dp[n][m]; // 如果整个字符串匹配成功,则dp[n][m]为true
    }
};

golang

func isMatch(s string, p string) bool {
    
    
    n, m := len(s), len(p)
    dp := make([][]bool, n+1)
    for i := 0; i <= n; i++ {
    
    
        dp[i] = make([]bool, m+1)
    }
    dp[0][0] = true // 当s和p都为空时,匹配成功
    for i := 0; i <= n; i++ {
    
    
        for j := 1; j <= m; j++ {
    
    
            if p[j-1] == '*' {
    
     // 如果p[j]是星号
                dp[i][j] = dp[i][j-2] // 如果星号匹配零个前面的元素,则有dp[i][j]=dp[i][j-2]
                if i > 0 && (p[j-2] == s[i-1] || p[j-2] == '.') {
    
     // 如果星号匹配多个前面的元素
                    dp[i][j] = dp[i][j] || dp[i-1][j] // 此时有dp[i][j]=dp[i][j-2]或dp[i-1][j]
                }
            } else if i > 0 && (s[i-1] == p[j-1] || p[j-1] == '.') {
    
     // 如果p[j]是字母或点
                dp[i][j] = dp[i-1][j-1] // 如果s[i]和p[j]相等,则有dp[i][j]=dp[i-1][j-1]
            }
        }
    }
    return dp[n][m] // 如果整个字符串匹配成功,则dp[n][m]为true
}

Guess you like

Origin blog.csdn.net/weixin_41093846/article/details/130145664