-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathRegularExpression.java
87 lines (78 loc) · 3.37 KB
/
RegularExpression.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
package regular_expression;
import org.junit.*;
import static org.junit.Assert.*;
public class RegularExpression {
/*
Regular Expression
AirBnB Interview Question
*/
public class Solution {
public boolean regMatch(String source, String pattern) {
if (pattern.length() == 0) return source.length() == 0;
if (pattern.length() == 1) {
if (source.length() > 1 || source.length() == 0) return false;
return source.charAt(0) == pattern.charAt(0);
}
if (source.length() != 0 && (pattern.charAt(0) == '.' || pattern.charAt(0) == source.charAt(0))) {
if (pattern.charAt(1) == '*') {
return regMatch(source.substring(1), pattern) || regMatch(source, pattern.substring(2));
} else if (pattern.charAt(1) == '+') {
return regMatch(source.substring(1), pattern.substring(2)) || regMatch(source.substring(1), pattern.substring(2));
} else {
return regMatch(source.substring(1), pattern.substring(1));
}
}
return pattern.charAt(1) == '*' && regMatch(source, pattern.substring(2));
}
}
/*
Regular Expression - Dynamic Programming
AirBnB Interview Question
*/
public class Solution_2 {
public boolean regMatch(String s, String p) {
if (s == null || p == null) return false;
boolean[][] dp = new boolean[p.length() + 1][s.length() + 1];
dp[0][0] = true;
for (int i = 1; i <= p.length(); i++) {
if (p.charAt(i - 1) == '*' && dp[i - 2][0]) dp[i][0] = true;
}
for (int i = 1; i <= p.length(); i++) {
for (int j = 1; j <= s.length(); j++) {
if (p.charAt(i - 1) == '.' || p.charAt(i - 1) == s.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else if (p.charAt(i - 1) == '*' || p.charAt(i - 1) == '+') {
if (p.charAt(i - 2) == '.' || p.charAt(i - 2) == s.charAt(j - 1)) {
if (p.charAt(i - 1) == '*') {
dp[i][j] = dp[i - 2][j] || dp[i - 2][j - 1] || dp[i][j - 1];
} else {
dp[i][j] = dp[i - 2][j - 1] || dp[i][j - 1];
}
} else {
dp[i][j] = p.charAt(i - 1) == '*' && dp[i - 2][j];
}
}
}
}
return dp[p.length()][s.length()];
}
}
public static class UnitTest {
@Test
public void test1() {
Solution sol = new RegularExpression().new Solution();
assertTrue(sol.regMatch("saaaa", "s+a*"));
assertFalse(sol.regMatch("saaaa", "s+b*"));
assertFalse(sol.regMatch("saaaab", "s+a*"));
assertFalse(sol.regMatch("saaaab", "s+b*"));
}
@Test
public void test2() {
Solution_2 sol = new RegularExpression().new Solution_2();
assertTrue(sol.regMatch("saaaa", "s+a*"));
assertFalse(sol.regMatch("saaaa", "s+b*"));
assertFalse(sol.regMatch("saaaab", "s+a*"));
assertFalse(sol.regMatch("saaaab", "s+b*"));
}
}
}