Sunday, August 16, 2015

Leetcode 44. Wildcard Matching

https://leetcode.com/problems/wildcard-matching/

Similar with problem No.10, but this time the one line solution is not applicable because the rules here is slightly different from regular expression.

Solution:
# T:O(n) S:O(1)
class Solution:
    # @param s, an input string
    # @param p, a pattern string
    # @return a boolean
    # @good solution! use 'aaaabaaaab' vs 'a*b*b' as an example
    def isMatch(self, s, p):
        pPointer = sPointer = ss = 0; star = -1
        # ss is star pointer in s, star is star pointer in p
        while sPointer < len(s):
            if pPointer < len(p) and (s[sPointer] == p[pPointer] or p[pPointer] == '?'):
                sPointer += 1; pPointer += 1
                continue
            if pPointer < len(p) and p[pPointer] == '*':
                star = pPointer; pPointer += 1; ss = sPointer;
                continue
            if star != -1:
                pPointer = star + 1; ss += 1; sPointer = ss
                continue
            return False
        while pPointer < len(p) and p[pPointer] == '*':
            pPointer += 1
        if pPointer == len(p): return True
        return False
Run Time: 132 ms

No comments:

Post a Comment