1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| bool isMatch(const char *s, const char *p)
def isMatch(s, p): if len(p) == 0: return len(s) == 0 if len(p) == 1: return len(s) == 1 and (s[0] == p[0] or p[0] == '.') if p[1] != '*': if len(s) > 0 and (s[0] == p[0] or p[0] == '.'): return isMatch(s[1:], p[1:]) else: return False while len(s) > 0 and (s[0] == p[0] or p[0] == '.'): if isMatch(s, p[2:]): return True s = s[1:]
return isMatch(s, p[2:])
if __name__ == "__main__": print(isMatch("abcdd", "a*bcd*")) print(isMatch("aab", "c*a*b"))
|