-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathRegularExpressionMatching.java
More file actions
44 lines (38 loc) · 1.43 KB
/
RegularExpressionMatching.java
File metadata and controls
44 lines (38 loc) · 1.43 KB
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
package com.thealgorithms.dynamicprogramming;
/**
* Class for Regular Expression Matching using Dynamic Programming.
*/
public class RegularExpressionMatching {
/**
* Determines if the input string matches the given pattern.
*
* @param s the input string
* @param p the pattern string (may contain '.' and '*')
* @return true if the string matches the pattern, false otherwise
*/
public boolean isMatch(final String s, final String p) {
final Boolean[][] memo = new Boolean[s.length() + 1][p.length() + 1];
return dp(0, 0, s, p, memo);
}
private boolean dp(final int i, final int j, final String s, final String p, final Boolean[][] memo) {
if (memo[i][j] != null) {
return memo[i][j];
}
final boolean ans;
if (j == p.length()) {
ans = (i == s.length());
} else {
final boolean firstMatch = (i < s.length() && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'));
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
// Two options:
// 1. Skip the pattern with '*'
// 2. Use '*' to match one character
ans = dp(i, j + 2, s, p, memo) || (firstMatch && dp(i + 1, j, s, p, memo));
} else {
ans = firstMatch && dp(i + 1, j + 1, s, p, memo);
}
}
memo[i][j] = ans;
return ans;
}
}