-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRegularExpressionMatching.java
204 lines (173 loc) · 5.77 KB
/
RegularExpressionMatching.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package LeetCodeJava.Array;
// https://leetcode.com/problems/regular-expression-matching/description/
/**
* 10. Regular Expression Matching
* Solved
* Hard
* Topics
* Companies
* 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:
*
* Input: s = "aa", p = "a"
* Output: false
* Explanation: "a" does not match the entire string "aa".
* Example 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:
*
* Input: s = "ab", p = ".*"
* Output: true
* Explanation: ".*" means "zero or more (*) of any character (.)".
*
*
* Constraints:
*
* 1 <= s.length <= 20
* 1 <= p.length <= 20
* s contains only lowercase English letters.
* p contains only lowercase English letters, '.', and '*'.
* It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
*
*
*/
public class RegularExpressionMatching {
// V0
// public boolean isMatch(String s, String p) {
//
// }
// V1-1
// https://neetcode.io/problems/regular-expression-matching
// IDEA: RECURSION
public boolean isMatch_1_1(String s, String p) {
int m = s.length(), n = p.length();
return dfs(0, 0, s, p, m, n);
}
private boolean dfs(int i, int j, String s, String p, int m, int n) {
if (j == n) return i == m;
boolean match = i < m && (s.charAt(i) == p.charAt(j) ||
p.charAt(j) == '.');
if (j + 1 < n && p.charAt(j + 1) == '*') {
return dfs(i, j + 2, s, p, m, n) ||
(match && dfs(i + 1, j, s, p, m, n));
}
if (match) {
return dfs(i + 1, j + 1, s, p, m, n);
}
return false;
}
// V1-2
// https://neetcode.io/problems/regular-expression-matching
// IDEA: DP (TOP DOWN)
private Boolean[][] dp;
public boolean isMatch_1_2(String s, String p) {
int m = s.length(), n = p.length();
dp = new Boolean[m + 1][n + 1];
return dfs_1_2(0, 0, s, p, m, n);
}
private boolean dfs_1_2(int i, int j, String s, String p, int m, int n) {
if (j == n) {
return i == m;
}
if (dp[i][j] != null) {
return dp[i][j];
}
boolean match = i < m && (s.charAt(i) == p.charAt(j) ||
p.charAt(j) == '.');
if (j + 1 < n && p.charAt(j + 1) == '*') {
dp[i][j] = dfs_1_2(i, j + 2, s, p, m, n) ||
(match && dfs_1_2(i + 1, j, s, p, m, n));
} else {
dp[i][j] = match && dfs_1_2(i + 1, j + 1, s, p, m, n);
}
return dp[i][j];
}
// V1-3
// https://neetcode.io/problems/regular-expression-matching
// IDEA: DP (BOTTOM UP)
public boolean isMatch_1_3(String s, String p) {
int m = s.length(), n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[m][n] = true;
for (int i = m; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
boolean match = i < m && (s.charAt(i) == p.charAt(j) ||
p.charAt(j) == '.');
if ((j + 1) < n && p.charAt(j + 1) == '*') {
dp[i][j] = dp[i][j + 2];
if (match) {
dp[i][j] = dp[i + 1][j] || dp[i][j];
}
} else if (match) {
dp[i][j] = dp[i + 1][j + 1];
}
}
}
return dp[0][0];
}
// V1-4
// https://neetcode.io/problems/regular-expression-matching
// IDEA: DP (SPACE OPTIMIZED)
public boolean isMatch_1_4(String s, String p) {
boolean[] dp = new boolean[p.length() + 1];
dp[p.length()] = true;
for (int i = s.length(); i >= 0; i--) {
boolean[] nextDp = new boolean[p.length() + 1];
nextDp[p.length()] = (i == s.length());
for (int j = p.length() - 1; j >= 0; j--) {
boolean match = i < s.length() &&
(s.charAt(i) == p.charAt(j) ||
p.charAt(j) == '.');
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
nextDp[j] = nextDp[j + 2];
if (match) {
nextDp[j] |= dp[j];
}
} else if (match) {
nextDp[j] = dp[j + 1];
}
}
dp = nextDp;
}
return dp[0];
}
// V1-5
// https://neetcode.io/problems/regular-expression-matching
// IDEA: DP (OPTIMAL)
public boolean isMatch_1_5(String s, String p) {
boolean[] dp = new boolean[p.length() + 1];
dp[p.length()] = true;
for (int i = s.length(); i >= 0; i--) {
boolean dp1 = dp[p.length()];
dp[p.length()] = (i == s.length());
for (int j = p.length() - 1; j >= 0; j--) {
boolean match = i < s.length() &&
(s.charAt(i) == p.charAt(j) ||
p.charAt(j) == '.');
boolean res = false;
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
res = dp[j + 2];
if (match) {
res |= dp[j];
}
} else if (match) {
res = dp1;
}
dp1 = dp[j];
dp[j] = res;
}
}
return dp[0];
}
// V2
}