10. Regular Expression Matching 正则表达式匹配

该博客讨论了LeetCode中的Regular Expression Matching问题,涉及如何使用递归和动态规划实现正则表达式的匹配功能。文章详细解释了两种解题思路,包括递归解法和动态规划解法,给出了具体的代码实现,并分析了各自的运行时间和内存消耗。

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

题目:Regular Expression Matching 正则表达式匹配

难度:困难

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题意解析:给定一个输入字符串s和正则字符串p,实现支持*和.的正则表达式匹配,*代表前一个字符出现0个或多个,.代表任意字符。

解题思路一:

递归

对可能出现的情况进行分析

1.当s和p都为空字符串时,此时返回true,

2.判断p的第二位是否为*,

  2.1,p.charAt(1)==‘*’,此时又可以分为两种情况

     2.1.1,*号匹配0个字符,此时我们只需将后移两位和s进行匹配即可,isMatch(s, p.substring(2))

     2.1.2,*号匹配多个字符,这时必须保证s.charAt(0)==p.charAt(0)或者p.charAt(0)=='.',此时我们继续将s后移一位与p进行匹配即可,isMatch(s.substring(1), p)

  2.2,p.charAt(1)!=‘*’,此时只需判断s.charAt(0)是否等于p.charAt(0)或者p.charAt(0)是否等于.,若相等,则将s和p同时后移一位进行再次匹配isMatch(s.substring(1), p.substring(1))

public boolean isMatch(String s, String p) {
        if (p.length() == 0) {
            return s.length() == 0;
        }
        if (p.length() > 1 && p.charAt(1) == '*') {  // second char is '*'
            if (isMatch(s, p.substring(2))) {
                return true;
            }
            if(s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
                return isMatch(s.substring(1), p);
            }
            return false;
        } else {                                     // second char is not '*'
            if(s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
                return isMatch(s.substring(1), p.substring(1));
            }
            return false;
        }
    }

提交代码之后:

Runtime: 51 ms, faster than 29.25% of Java online submissions for Regular Expression Matching.

Memory Usage: 42.2 MB, less than 18.34% of Java online submissions for Regular Expression Matching.

解题思路二:

动态规划

这种解法较之上面更加难以理解,当初博主也是百度了很多资料才弄懂。

考察每个字符的匹配状态,这里我们选择一个bool类型的数组,true表示匹配。

用s的长度m和p的长度n分别表示行和列的长度。

boolean[][] dp = new boolean[m+1][n+1];

显然 dp[0][0]=true,表示s和p在这个状态时都为空字符串。

下面对边界情况进行赋值:

对于第一列,dp[i][0],只有当i=0时,为true,其余都为false,

对于第一行,dp[0][j],只有当dp[0][j - 2]为true,并且p.charAt(j-1)='*',才会为true,如a*,a*b*c*这种格式,因为可以让abc重复0次。

下面针对一般的情况进行讨论:

1.当p.charAt(j-1) != '*'时,此时只需p.charAt(j-1) == '.' || s.charAt(i-1) == p.charAt(j-1) && dp[i - 1][j - 1]

2.当p.charAt(j-1) == '*'时,分三种情况进行讨论,

  2.1 *号重复零次,那么此时只需dp[i][j - 2]为true就行,

  2.2 *号重复1次,那么此时只需dp[i][j-1]为true就行

  2.2 *号重复多次,此时只需s(0,i-1)和p(0,j)匹配,并且此时也匹配了至少一次,s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.'。

综上所述:

public boolean isMatch4(String s, String p) {
        int m = s.length(),n = p.length();
        boolean[][] dp = new boolean[m+1][n+1];
        dp[0][0] = true;
        for (int i = 1; i <= m; i++)
            dp[i][0] = false;
        for (int j = 1; j <= n; j++)
            dp[0][j] = j > 1 && '*' == p.charAt(j-1)&& dp[0][j - 2];
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (p.charAt(j-1) == '*')
                {
                    dp[i][j] = dp[i][j - 2] || (s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.') && dp[i - 1][j];

                }
                else 
                {
                    dp[i][j] = (p.charAt(j-1) == '.' || s.charAt(i-1) == p.charAt(j-1)) && dp[i - 1][j - 1];

                }
            }
        }
        return dp[m][n];
    }

提交代码之后:

Runtime: 2 ms, faster than 99.33% of Java online submissions for Regular Expression Matching.

Memory Usage: 37.7 MB, less than 92.86% of Java online submissions for Regular Expression Matching.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值