Lintcode -正则表达式

实现支持'.'和'*'的正则表达式匹配。

'.'匹配任意一个字母。

'*'匹配零个或者多个前面的元素。

匹配应该覆盖整个输入字符串,而不仅仅是一部分。

需要实现的函数是:bool isMatch(const char *s, const char *p)

您在真实的面试中是否遇到过这个题? Yes
样例
isMatch("aa","a") → false

isMatch("aa","aa") → true

isMatch("aaa","aa") → false

isMatch("aa", "a*") → true

isMatch("aa", ".*") → true

isMatch("ab", ".*") → true

isMatch("aab", "cab") → true

Python版:

import re
class Solution:
    """
    @param s: A string 
    @param p: A string includes "." and "*"
    @return: A boolean
    """
    def isMatch(self, s, p):
        try:
            p = re.compile(p)
            r = p.findall(s)
            if r[0] == s and (len(r)==1 or (len(r)==2 and r[1]=='')):
                return True
            else:
                return False
        except:
            return False

你可能感兴趣的:(Lintcode -正则表达式)