Regular Expression Matching

题目描述:

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).

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", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

思路就是递归法,代码如下:

public boolean isMatch(String s, String p) {
    if (p.length() == 0) {
        return s.length() == 0;
    }
    if (p.length() > 1 && p.charAt(1) == '*') { 
        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 {                                     
        if(s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
            return isMatch(s.substring(1), p.substring(1));
        }
        return false;
    }
}

还有一种风格的递归:

public class Solution {
    public boolean matches(String s,int sIndex,String p,int pIndex) {
        if (pIndex == p.length()) {
            return sIndex == s.length();
        }
        if (pIndex < p.length()-1 && p.charAt(pIndex+1) == '*') {
            while (true) {
                if (matches(s,sIndex,p,pIndex+2)) {
                    return true;
                }
                if (sIndex < s.length() && (s.charAt(sIndex)==p.charAt(pIndex) || p.charAt(pIndex)=='.')) {
                    sIndex++;
                }
                else {
                    return matches(s,sIndex,p,pIndex+2);
                }
            }
        }
        if (sIndex == s.length()) {
            return false;
        }
        if (p.charAt(pIndex)=='.' || p.charAt(pIndex) == s.charAt(sIndex)) {
            return matches(s,sIndex+1,p,pIndex+1);
        }
        return false;
    }
    
    public boolean isMatch(String s, String p) {
        // try matching with asterisk
        if (p == null || p.length()==0) return (s==null || s.length()==0);
        return matches(s,0,p,0);
    }
}
然而这个题最快的方法应该是用动态规划来优化算法:

public class Solution {

    char[] s, p;
    int sLength, pLength, noWildcard;
    Boolean[][] dp;

    public boolean isMatch(String ss, String ps) {
        s = ss.toCharArray();
        p = ps.toCharArray();
        sLength = s.length;
        pLength = p.length;
        noWildcard = pLength - 1;
        dp = new Boolean[pLength + 1][sLength + 1];
        return isMatch(0, 0);
    }

    boolean isMatch(int pi, int si){
        if(dp[pi][si] != null) return dp[pi][si];
        while(true){
            if(pi < noWildcard && p[pi+1] == '*'){
                char c = p[pi];
                pi += 2;
                while(true){
                    boolean isMatch = isMatch(pi, si);
                    dp[pi][si] = isMatch;
                    if(isMatch){
                        return true;
                    }
                    if(si == sLength) return pi == pLength;
                    if(c != '.' && c != s[si]){
                        return false;
                    }
                    si++;
                }
            }
            if(pi == pLength || si == sLength) return pi == pLength && si == sLength;
            if(p[pi] != '.' && p[pi] != s[si]){
                return false;
            }
            pi++;
            si++;
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值