Created
June 2, 2017 15:43
-
-
Save i-van/2316dd196ae951976a0d7334e0a23b73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isMatchHelper(text, pattern, textIndex, patternIndex): | |
if textIndex >= len(text): | |
if patternIndex >= len(pattern): | |
return True | |
elif (patternIndex+1 < len(pattern)) and (pattern[patternIndex+1] == '*'): | |
return isMatchHelper(text, pattern, textIndex, patternIndex + 2) | |
else: | |
return False | |
elif (patternIndex >= len(pattern)) and (textIndex < len(text)): | |
return False | |
elif (patternIndex+1 < len(pattern)) and (pattern[patternIndex+1] == '*'): | |
if (pattern[patternIndex] == '.') or (text[textIndex] == pattern[patternIndex]): | |
return isMatchHelper(text, pattern, textIndex, patternIndex + 2) or isMatchHelper(text, pattern, textIndex + 1, patternIndex) | |
else: | |
return isMatchHelper(text, pattern, textIndex, patternIndex + 2) | |
elif (pattern[patternIndex] == '.') or (pattern[patternIndex] == text[textIndex]): | |
return isMatchHelper(text, pattern, textIndex + 1, patternIndex + 1) | |
else: | |
return False | |
isMatch = lambda t,p: isMatchHelper(t,p,0,0) | |
print(isMatch("","")) | |
print(isMatch("aa","a")) | |
print(isMatch("bb","bb")) | |
print(isMatch("","a*")) | |
print(isMatch("abbdbb","ab*d")) | |
print(isMatch("aba","a.a")) | |
print(isMatch("acd","ab*c.")) | |
print(isMatch("abaa","a.*a*")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment