正则表达式匹配

剑指offer原题:
请实现一个函数用来匹配包括’.’和’*’的正则表达式。模式中的字符’.’表示任意一个字符,而’*’表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配

思路,回溯,关键是处理下一个字符是“*”的情况,如果下一个字符是“*”,且当前字符匹配(下一个字符是“*”,当前不匹配只有一种情况),分三种情况讨论:
1. 忽略“*”(这种情况是使其前面的字符出现1次),这样的话,源字符串+1,模式字符串+2。
2. 认为“*”的作用是使其前面的字符出现0次,此时源字符串+0,模式字符串+2
3. 认为“*”的作用是使其前面的字符出现多次,此时源字符串+1,模式字符串不变。

代码如下:

    public boolean match(char[] str, char[] pattern) {
        if(str==null || pattern==null){return false;}
        return matchCore(str,pattern,0,0);
    }

    public boolean matchCore(char[] str,char[] pattern,int i,int j){

        if(i==str.length && j==pattern.length){     //同时到达终点
            return true;
        }
        //pattern已经到达终点,没有伸缩的可能,如果str先到达终点,但是pattern后面时*号的话还是有可能仍然成立的
        if(i<str.length && j==pattern.length){     
            return false;
        }else if(i==str.length){
            return chechNull(pattern,j);
        }

        if(j+1<pattern.length && pattern[j+1]=='*'){  //模式后一个是*号
            if(str[i]==pattern[j] ||(pattern[j]=='.' && i<str.length)){
                boolean res = matchCore(str,pattern,i+1,j+2) || matchCore(str,pattern,i,j+2) || matchCore(str,pattern,i+1,j);
                return  res;
            }else{
                return matchCore(str,pattern,i,j+2);//不匹配,只能把*当做出现零次
            }
        }

        if(str[i]==pattern[j] ||(pattern[j]=='.' && i<str.length)){
            return matchCore(str,pattern,i+1,j+1);
        }

        return false;
    }

    //用于源字符串检测完了,判断模式字符串后面是不是空串
    public boolean chechNull(char[] pattern,int j){

        if(pattern[pattern.length-1] !='*'){
            return false;
        }
        for(int k=j;k<pattern.length;k++){
            if(pattern[k]!='*' && k+1<pattern.length && pattern[k+1]!='*'){
                return false;
            }
        }
        return true;
    }

leetcode上有一个题跟这个类似,
44 Wildcard Matching

Implement wildcard pattern matching with support for ‘?’ and ‘*’.
‘?’ Matches any single character.
‘*’ Matches any sequence of characters (including the empty sequence).

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

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “*”) → true
isMatch(“aa”, “a*”) → true
isMatch(“ab”, “?*”) → true
isMatch(“aab”, “c*a*b”) → false

https://leetcode.com/problems/wildcard-matching/

仿照上面的题目写了一下源代码,结果超时,这个题一般使用动态规划。

public boolean isMatch_2d_method(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++) {
        if(p.charAt(j-1)=='*'){
            dp[0][j] = true;
        } else {
            break;
        }
    }

    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            if (p.charAt(j-1)!='*') {
                dp[i][j] = dp[i-1][j-1] && (s.charAt(i-1)==p.charAt(j-1) || p.charAt(j-1)=='?');
            } else {
                dp[i][j] = dp[i-1][j] || dp[i][j-1];
            }
        }
    }
    return dp[m][n];
}

解释如下,其实我也不是很明白:
https://leetcode.com/discuss/54278/my-java-dp-solution-using-2d-table
dp[i][j] denotes whether s[0....i-1] matches p[0.....j-1],

关键在于理解dp[i][j] = dp[i-1][j] || dp[i][j-1];
我的理解是,如果模式串中第j个字符为“*”,那么有两种情况符合那就是匹配的,如下
1. 忽略这个*,这个*表示一个空,那么就是这种情况dp[i-1][j]
2. 这个“*”表示一个字符,这个字符就是源字符串的第i个字符,也就是让其匹配,那么就是这种情况dp[i][j-1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值