LC 10 - Regular Expression Matching
LC 10 - Regular Expression Matching
Question
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
'.'Matches any single character.'*'Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Example 1:
1
2
Input: s = "aa", p = "a"
Output: false
Explanation: “a” does not match the entire string “aa”.
Example 2:
1
2
Input: s = "aa", p = "a*"
Output: true
Explanation: ‘*’ means zero or more of the preceding element, ‘a’. Therefore, by repeating ‘a’ once, it becomes “aa”.
Example 3:
1
2
Input: s = "ab", p = ".*"
Output: true
Explanation: “.” means “zero or more () of any character (.)”.
Constraints:
1 <= s.length <= 201 <= p.length <= 20scontains only lowercase English letters.pcontains only lowercase English letters,'.', and'*'.- It is guaranteed for each appearance of the character
'*', there will be a previous valid character to match.
Links
Question here and solution here
Solution
concept
We can use a decision tree for this question and there are a few cases we need to consider:
- if
s[i] == p[j]exactly orp[j] == ".", then we have found a match, this means(i, j) -> (i + 1, j + 1) - if we have encounter a
*, there are two sub cases:- if we use the
*(and precedent char matches), this means that we have found a match by repeating the precedent char, then(i, j) -> (i + 1, j),jremains the same since we could use the*again - if we do not use the
*, this means we are skipping the current char and the*, this means(i, j) -> (i, j + 2)
- if we use the
The base case is such that:
- if both
iandjare out of bound, then we fully matched and the answer isTrue - if only
jis out of bound from strp, while there are still char ins, then this isFalse - However, if only
iis out of bound and there are still char inp, it still could beTruesince we might have*which we can use it as repeat 0 times, effectively delete all the remaining char inp.
code
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution:
"""
brute force DFS
"""
def isMatch(self, s: str, p: str) -> bool:
def dfs(i,j):
if i >= len(s) and j >= len(p):
return True
if j >= len(p):
return False
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
return dfs(i, j + 2) or (match and dfs(i + 1, j)) # dont use * OR use *
if match:
return dfs(i + 1, j + 1)
return False
return dfs(0,0)
class Solution:
"""
top down: memoization
"""
def isMatch(self, s: str, p: str) -> bool:
cache = {}
def dfs(i,j):
if i >= len(s) and j >= len(p):
return True
if j >= len(p):
return False
if (i, j) in cache:
return cache[(i,j)]
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
cache[(i,j)] = dfs(i, j + 2) or (match and dfs(i + 1, j)) # dont use * OR use *
return cache[(i,j)]
if match:
cache[(i,j)] = dfs(i + 1, j + 1)
return cache[(i,j)]
cache[(i,j)] = False
return cache[(i,j)]
return dfs(0,0)
class Solution:
"""
bottom up solution
"""
def isMatch(self, s: str, p: str) -> bool:
dp = [[False] * (len(p) + 1) for i in range(len(s) + 1)]
dp[len(s)][len(p)] = True
for i in range(len(s), -1, -1):
for j in range(len(p) - 1, -1, -1):
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
if (j + 1) < len(p) and p[j + 1] == "*":
dp[i][j] = dp[i][j + 2]
if match:
dp[i][j] = dp[i + 1][j] or dp[i][j]
elif match:
dp[i][j] = dp[i + 1][j + 1]
return dp[0][0]
Complexity
time: $O(mn)$
space: $O(mn)$
This post is licensed under CC BY 4.0 by the author.