From b031f82d97d495ef7b256295b9cbb238ba4a1819 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Wed, 11 Jan 2023 20:59:06 -0500 Subject: [PATCH] Time: 1054 ms (28.24%), Space: 13.9 MB (55.17%) - LeetHub --- .../10-regular-expression-matching.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 10-regular-expression-matching/10-regular-expression-matching.py diff --git a/10-regular-expression-matching/10-regular-expression-matching.py b/10-regular-expression-matching/10-regular-expression-matching.py new file mode 100644 index 0000000..71421e2 --- /dev/null +++ b/10-regular-expression-matching/10-regular-expression-matching.py @@ -0,0 +1,34 @@ +class Solution: + def isMatch(self, s: str, p: str) -> bool: + # Recursive solution + + # Base Case + if s == "" or p == "": + if s == "" and p == "": + return True + # Check for "*" = 0 elements + elif s == "" and len(p) >= 2 and p[1] == "*": + return self.isMatch(s,p[2:]) + else: + return False + + # Dealing with "*" + if len(p) >= 2 and p[1] == "*": + + # Next character compatable with wildcard + if s[0] == p[0] or p[0] == ".": + return (self.isMatch(s,p[2:]) or # End wildcard + self.isMatch(s[1:],p)) # Use character as wildcard + else: + return self.isMatch(s,p[2:]) + + + # Dealing with "." + if p[0] == ".": + return self.isMatch(s[1:],p[1:]) + + + # Check first letters, recurse on the rest + if s[0] == p[0]: + return self.isMatch(s[1:],p[1:]) + \ No newline at end of file