Sunday, June 8, 2014

Leetcode (Python): Regular Expression Matching

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") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

Solution:

Using dynamic programming (instead of a matrix that keeps if s[0..row] matches p[0..col], but to reduce the space complexity we just need to keep the previous row).

class Solution:
    # @return a boolean
    def isMatch(self, s, p):
        previousRow = [True]
        for i in range(0, len(p)):
            if p[i]=='*':
                previousRow.append(previousRow[i-1])
            else:
                previousRow.append(False)
                
        for letter in range(0,len(s)):
            actualRow = [False];
            for i in range(0, len(p)):
                if p[i]=='*':
                    temp = actualRow[i-1] or (previousRow[i+1] and (p[i-1]==s[letter] or p[i-1]=='.'))
                elif p[i] == '.':
                    temp = previousRow[i]
                else:
                    temp = previousRow[i] and p[i]==s[letter] 
                actualRow.append(temp)
            previousRow = actualRow
        return previousRow[len(p)]
                

No comments :

Post a Comment