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

來源:互聯網
上載者:User

標籤:leetcode   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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
Solution:多種方法解決,可以用DFS,也可以用DP,參考這裡:http://blog.csdn.net/hcbbt/article/details/44016237題目大意:給定一個字串和一個Regex,給出正則是否匹配字串解題思路:關鍵問題在*上面,*可以佔用零個或者多個位置,因此用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



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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.