【LeetCode】10. Regular Expression Matching

本文介绍了一种正则表达式匹配算法的两种实现方法:递归和动态规划。通过具体的例子展示了如何处理包含 '.' 和 '*' 的模式匹配,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false


Solution 1: 30.47%

This problem is to determine if starting from index 0, string s matches string p or not. Using recursion. In general, Let's start from sindex and pindex. There are two cases: Case 1: next char of pindex is '*': '*' can match 0 or more times,  any one senario matches, then s and p can match. First, '*' match 0 times, s and p matches when s starting from sindex matches p starting from pindex+2. Second, '*' match one or more times, since we don't know how many times '*' matches, we write a for loop with index i++ in string s, first we have to make sure s.charAt(i)==p.charAt(pindex), once the rest of string s matches the rest of string p, then string s matches string p, otherwise i++ to match one more time. When we reach the end of string s, and no matches during the loop, then s and p cannot match. Case 2: next char of pindex is not '*', so s and p matches when current char of s match current char of p, and s and p matches starting from sindex+1 and pindex+1. 

Code:

public boolean isMatch(String s, String p) {
        if (s == null || p == null){
            return false;
        }
        return match(s, p, 0, 0);
    }
    
    private boolean match(String s, String p, int sindex, int pindex){
        if (pindex == p.length()){
            return sindex == s.length();
        }
        if (pindex + 1 < p.length() && p.charAt(pindex + 1) == '*'){
            // * match 0 times
            if (match(s, p, sindex, pindex + 2)){
                return true;
            }
            // match 1 or more times
            for (int i = sindex; i < s.length(); i++){
                if (!charMatch(s.charAt(i), p.charAt(pindex))){
                    return false;
                }
                if (match(s, p, i + 1, pindex + 2)){
                    return true;
                }
            }
            return false;
        }
        return sindex < s.length() && charMatch(s.charAt(sindex), p.charAt(pindex)) && match(s, p, sindex + 1, pindex + 1);
    }
    
    private boolean charMatch(char ss, char pp){
       if (pp == '.'){
           return true;
       }
        if (pp == '*'){
            return false;
        }
        return ss == pp;
    }
Solution 2: 75.92%
This problem can be solved by dynamic programming.

Let's say dp[i][j] means if string s with length i matches string p with length j or not. 

1. s == "", p == "", both are length 0, match.

2. s == "", p != "", s with length 0, p matches s when jth element is '*', match 0 times, and previous substring of p matches s.

3. s != "", p == "", this case s and p would never match.

4. s != "", p != ""

    a. ith element of s match jth element of p, dp[i][j] is determined by dp[i-1][j-1]

    b. ith element of s not match jth element of p

        1. j th element of p is not '*', s and p cannot match

        2. j th element of p is '*', 

           if j-1 th element of p == ith element of s, then '*' can match 0 or 1 or multiple times, if any senario match, the s             and p can match.

                -> match 0 times, dp[i][j] is determined by dp[i][j-2]

                -> match 1 times, dp[i][j] is determined by dp[i-1][j-2]

             -> match multiple times, Since '*' match multiple times, let's see dp[i-1][j] is match or not, if yes, then dp[i][j]                 can match, otherwise not. eg. "aaaaaaaa", ".*"

            if j-1 th element of p != ith element of s, then '*' can only match 0 times to eliminate (j-1 and j) th elements, and then to see if dp[i][j - 2] match or not.

Final the result is dp[s.length][p.length]

Code:

public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
        
        // s:"" matches p:""
        dp[0][0] = true;
        
        // p:""
        for (int i = 1; i <= s.length(); i++){
            dp[i][0] = false;
        }
        // s:"", in this case, p matches s
        for (int j = 1; j <= p.length(); j++){
            if (p.charAt(j - 1) == '*' && dp[0][j - 2]){
                dp[0][j] = true;
            }
        }
        // s != "", p != ""
        for (int i = 1; i <= s.length(); i++){
            for (int j = 1; j <= p.length(); j++){
                // if current char in s matches current char in p
                if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.'){
                    dp[i][j] = dp[i - 1][j - 1];
                } else if (p.charAt(j - 1) == '*'){ // if current char c in p is *, could match
                    // if previous char c1 in p not matches current char in s, c and c1 become ""
                    if (p.charAt(j - 2) != '.' && p.charAt(j - 2) != s.charAt(i - 1)){
                        dp[i][j] = dp[i][j - 2];
                    } else {
                        // * match 0 times or once or multiple times
                        dp[i][j] = dp[i][j - 2] || dp[i - 1][j - 2] || dp[i - 1][j]; // not dp[i-1][j-1]:see 'aaa', '.*'
                    }
                }
            }
        }
        return dp[s.length()][p.length()];
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值