LeetCode 10 Regular Expression Matching (C,C++,Java,Python)

Problem:

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

Solution:

多种方法解决,可以用DFS,也可以用DP,参考这里:http://blog.csdn.net/hcbbt/article/details/44016237

题目大意:

给定一个字符串和一个正则表达式,给出正则是否匹配字符串

解题思路:

关键问题在*上面,*可以占用零个或者多个位置,因此用DFS的办法就是尝试遍所有的可能匹配的情况,比如AAAAAB和A*B,就要看AAAAAB和B还有AAAB和B和AB和B

Java源代码(用时302ms):

public class Solution {
    public boolean isMatch(String s, String p) {
        char[] chs = s.toCharArray();
        char[] chp = p.toCharArray();
        return Match(chs,0,chp,0);
    }
    public boolean Match(char[] chs,int index1,char[] chp,int index2){
        if(index2>=chp.length)return index1>=chs.length;
        if(index2+1<chp.length && chp[index2+1]=='*'){
            while(index1<chs.length && (chp[index2]=='.' || chp[index2]==chs[index1])){
                if(Match(chs,index1,chp,index2+2))return true;
                index1++;
            }
            return Match(chs,index1,chp,index2+2);
        }else if(index1<chs.length && (chp[index2]=='.' || chs[index1]==chp[index2])){
            return Match(chs,index1+1,chp,index2+1);
        }
        return false;
    }
}

C语言源代码(用时21ms):

bool isMatch(char* s, char* p) {
    if(s==NULL || p==NULL)return false;
    if(!*p) return !*s;
    if(*(p+1)=='*'){
        while((*p==*s)||(*s && *p=='.')){
            if(isMatch(s,p+2))return true;
            s++;
        }
        return isMatch(s,p+2);
    }else if((*p==*s)||(*s && *p=='.')){
        return isMatch(s+1,p+1);
    }
    return false;
}

C++源代码(用时407ms):

class Solution {
public:
    bool isMatch(string s, string p) {
        return Match(s,0,p,0);
    }
    bool Match(string s,int index1,string p,int index2){
        if(index2>=p.size())return index1>=s.size();
        if(index2+1<p.size() && p[index2+1]=='*'){
            while(index1<s.size() && (p[index2]=='.' || p[index2]==s[index1])){
                if(Match(s,index1,p,index2+2))return true;
                index1++;
            }
            return Match(s,index1,p,index2+2);
        }else if(index1<s.size() && (p[index2]=='.' || p[index2]==s[index1])){
            return Match(s,index1+1,p,index2+1);
        }
        return false;
    }
};

Python源代码(调用自带函数用时140ms):

class Solution:
    # @param {string} s
    # @param {string} p
    # @return {boolean}
    def isMatch(self, s, p):
        return re.match('^' + p + '$', s) != None

Python源代码(DFS超时):

class Solution:
    # @param {string} s
    # @param {string} p
    # @return {boolean}
    def isMatch(self, s, p):
        return self.Match(s,0,p,0)
    def Match(self,s,index1,p,index2):
        if index2>=len(p):return index1>=len(s)
        if index2+1<len(p) and p[index2+1]=='*':
            while index1<len(s) and (p[index2]=='.' or p[index2]==s[index1]):
                if self.Match(s,index1,p,index2+2):return True
                index1+=1
            return self.Match(s,index1,p,index2+2)
        elif index1<len(s) and (p[index2]=='.' or p[index2]==s[index1]):
            return self.Match(s,index1+1,p,index2+1)
        return False

时间: 2024-10-06 02:39:18

LeetCode 10 Regular Expression Matching (C,C++,Java,Python)的相关文章

[LeetCode] 010. Regular Expression Matching (Hard) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 010.Regular_Expression_Matching (Hard) 链接: 题目:https://oj.leetcode.com/problems/regular-expression-matching/ 代码(github):https://github.com/illuz/leetcode 题意: 给

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

[LeetCode][10]Regular Expression Matching解析 -Java实现

Q: 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

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 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 entir

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/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 ent

[LeetCode] 10. 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

LeetCode 10 Regular Expression Matching (正则表达式匹配)

翻译 实现支持"."和"*"的正则表达式匹配. "." 匹配支持单个字符 "*" 匹配零个或多个前面的元素 匹配应该覆盖到整个输入的字符串(而不是局部的). 该函数的原型应该是: bool isMatch(const char * s, const char * p) 示例: isMatch("aa","a") → false isMatch("aa","a