Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. class Solution(object):
  2.    
  3.     def isMatch(self, s, p):
  4.         """
  5.        :type s: str
  6.        :type p: str
  7.        :rtype: bool
  8.        """
  9.         self.memo = dict()
  10.         return self.isMatch2(s,p,0,0)
  11.        
  12.     def isMatch2(self, s, p, sindex, pindex):
  13.         # Base cases
  14.         #print(s[sindex:],p[pindex:])
  15.         if (sindex, pindex) in self.memo:
  16.             return self.memo[sindex,pindex]
  17.        
  18.         if len(p) == pindex:
  19.             rval = len(s) == sindex
  20.             self.memo[sindex,pindex] = rval
  21.             return rval
  22.        
  23.         first_match = sindex < len(s) and p[pindex] in [s[sindex], '.']
  24.        
  25.         #continuing cases
  26.         if pindex < len(p) - 1 and p[pindex + 1] == '*':
  27.             # first see if not-matching the starred character is possible
  28.             if self.isMatch2(s, p, sindex, pindex + 2):
  29.                 self.memo[sindex, pindex] = True
  30.                 return True
  31.             else:
  32.                 rval = first_match and self.isMatch2(s,p, sindex + 1, pindex)
  33.                 self.memo[sindex, pindex] = rval
  34.                 return rval
  35.         else:
  36.             rval = first_match and self.isMatch2(s,p, sindex + 1, pindex + 1)
  37.             self.memo[sindex,pindex]  = rval
  38.             return rval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement