1+ """
2+ # REGULAR EXPRESSION MATCHING
3+
4+ Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*' where:
5+
6+ '.' Matches any single character.
7+ '*' Matches zero or more of the preceding element.
8+ The matching should cover the entire input string (not partial).
9+
10+
11+
12+ Example 1:
13+
14+ Input: s = "aa", p = "a"
15+ Output: false
16+ Explanation: "a" does not match the entire string "aa".
17+ Example 2:
18+
19+ Input: s = "aa", p = "a*"
20+ Output: true
21+ Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
22+ Example 3:
23+
24+ Input: s = "ab", p = ".*"
25+ Output: true
26+ Explanation: ".*" means "zero or more (*) of any character (.)".
27+ Example 4:
28+
29+ Input: s = "aab", p = "c*a*b"
30+ Output: true
31+ Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
32+ Example 5:
33+
34+ Input: s = "mississippi", p = "mis*is*p*."
35+ Output: false
36+
37+
38+ Constraints:
39+
40+ 0 <= s.length <= 20
41+ 0 <= p.length <= 30
42+ s contains only lowercase English letters.
43+ p contains only lowercase English letters, '.', and '*'.
44+ It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
45+ """
46+ 47+ class Solution :
48+ def isMatch (self , s : str , p : str ) -> bool :
49+ 50+ table = [[None for _ in range (len (p ) + 1 )] for _ in range (len (s ) + 1 )]
51+ table [0 ][0 ] = True
52+ 53+ 54+ for row in range (1 , len (s ) + 1 ):
55+ table [row ][0 ] = False
56+ 57+ for col in range (1 , len (p ) + 1 ):
58+ if p [col - 1 ] == "*" :
59+ table [0 ][col ] = table [0 ][col - 2 ]
60+ else :
61+ table [0 ][col ] = False
62+ 63+ for row in range (1 , len (s ) + 1 ):
64+ for col in range (1 , len (p ) + 1 ):
65+ if p [col - 1 ] == "." :
66+ table [row ][col ] = table [row - 1 ][col - 1 ]
67+ elif p [col - 1 ] != "*" :
68+ table [row ][col ] = table [row - 1 ][col - 1 ] and s [row - 1 ] == p [col - 1 ]
69+ else :
70+ if p [col - 2 ] == "." :
71+ table [row ][col ] = table [row ][col - 1 ] or table [row ][col - 2 ] or table [row - 1 ][col - 1 ] or table [row - 1 ][col ]
72+ else :
73+ table [row ][col ] = table [row ][col - 1 ] or table [row ][col - 2 ] or (table [row - 1 ][col - 1 ] and p [col - 2 ] == s [row - 1 ])
74+ 75+ for row in range (len (s ) + 1 ):
76+ print (table [row ])
77+ 78+ return table [- 1 ][- 1 ]
0 commit comments