Friday, September 19, 2014

Leetcode: Wildcard Matching @Python

class Solution:
    # @param s, an input string
    # @param p, a pattern string
    # @return a boolean
    def isMatch(self, s, p):
        n=len(s)
        m=len(p)
        i=0
        j=0
        star=0
        s_coor=None
        while i<n:
            if j<m and (s[i]==p[j] or p[j]=='?'):
                j+=1
                i+=1
            elif j<m and p[j]=='*':
                s_coor=i
                star=j
                j+=1
            elif s_coor!=None:
                i=s_coor+1
                j=star+1
                s_coor+=1
            else:
                return False
        while j<m and p[j]=='*':
            j+=1
        if j==m:
            return True
        else:
            return False

No comments :

Post a Comment