Regular Expression

题目描述

解题方法

Solution

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        if len(p) == 0:
            return len(s) == 0
        if len(p) == 1 or p[1] != "*":
            if len(s) == 0 or (s[0] != p[0] and p[0] != "."):
                return False
            return self.isMatch(s[1:], p[1:])
        else:
            # start from match 0 to length characters
            i = -1
            while i < length and (i < 0 or p[0] == '.' or p[0] == s[i]):
                if self.isMatch(s[i+1:], p[2:]):
                    return True
                i += 1
            return False

Reference