-
Notifications
You must be signed in to change notification settings - Fork 90
/
RegularExpressionMatching10.java
152 lines (129 loc) · 5.06 KB
/
RegularExpressionMatching10.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
/**
* Implement regular expression matching with support for '.' and '*'.
*
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
* The matching should cover the entire input string (not partial).
*
* The function prototype should be:
* bool isMatch(const char *s, const char *p)
*
* Some examples:
* isMatch("aa","a") → false
* isMatch("aa","aa") → true
* isMatch("aaa","aa") → false
* isMatch("aa", "a*") → true
* isMatch("aa", ".*") → true
* isMatch("ab", ".*") → true
* isMatch("aab", "c*a*b") → true
*
*/
public class RegularExpressionMatching10 {
/**
* https://leetcode.com/problems/regular-expression-matching/solution/
*/
public boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) return text.isEmpty();
boolean first_match = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));
if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
return (isMatch(text, pattern.substring(2)) ||
(first_match && isMatch(text.substring(1), pattern)));
} else {
return first_match && isMatch(text.substring(1), pattern.substring(1));
}
}
public boolean isMatch2(String text, String pattern) {
return isMatch(0, 0, text, pattern);
}
public boolean isMatch(int i, int j, String text, String pattern) {
if (j == pattern.length()) return i == text.length();
boolean first_match = (i != text.length() &&
(pattern.charAt(j) == text.charAt(i) || pattern.charAt(j) == '.'));
if (pattern.length() >= j+2 && pattern.charAt(j+1) == '*'){
return (isMatch(i, j+2, text, pattern) ||
(first_match && isMatch(i+1, j, text, pattern)));
} else {
return first_match && isMatch(i+1, j+1, text, pattern);
}
}
public boolean isMatch3(String text, String pattern) {
int textLen = text.length();
int pattLen = pattern.length();
boolean[][] dp = new boolean[textLen+1][pattLen+1];
dp[0][0] = true;
for(int j=1; j<=pattLen; j++) {
if (pattern.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-2];
}
}
for (int i=1; i<=textLen; i++) {
for(int j=1; j<=pattLen; j++) {
if (j >= 2 && pattern.charAt(j-1) == '*') {
boolean preMatch = (pattern.charAt(j-2) == text.charAt(i-1) || pattern.charAt(j-2) == '.');
dp[i][j] = dp[i][j-2] || (preMatch && dp[i-1][j]);
} else {
boolean currMatch = (pattern.charAt(j-1) == text.charAt(i-1) || pattern.charAt(j-1) == '.');
dp[i][j] = currMatch && dp[i-1][j-1];
}
}
}
return dp[text.length()][pattern.length()];
}
/**
* https://leetcode.com/problems/regular-expression-matching/solution/
*/
enum Result {
TRUE, FALSE
}
Result[][] memo;
public boolean isMatch4(String text, String pattern) {
memo = new Result[text.length() + 1][pattern.length() + 1];
return dp(0, 0, text, pattern);
}
public boolean dp(int i, int j, String text, String pattern) {
if (memo[i][j] != null) {
return memo[i][j] == Result.TRUE;
}
boolean ans;
if (j == pattern.length()){
ans = i == text.length();
} else{
boolean first_match = (i < text.length() &&
(pattern.charAt(j) == text.charAt(i) ||
pattern.charAt(j) == '.'));
if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
ans = (dp(i, j+2, text, pattern) ||
first_match && dp(i+1, j, text, pattern));
} else {
ans = first_match && dp(i+1, j+1, text, pattern);
}
}
memo[i][j] = ans ? Result.TRUE : Result.FALSE;
return ans;
}
public boolean isMatch5(String s, String p) {
if (s == null || p == null) return false;
boolean[][] dp = new boolean[s.length()+1][p.length()+1];
dp[0][0] = true;
for (int j=1; j<=p.length(); j++) dp[0][j] = p.charAt(j-1) == '*' ? dp[0][j-2] : false;
for (int i=1; i<=s.length(); i++) dp[i][0] = false;
for (int i=1; i<=s.length(); i++) {
for (int j=1; j<=p.length(); j++) {
char c = p.charAt(j-1);
switch (c) {
case '.':
dp[i][j] = dp[i-1][j-1];
break;
case '*':
dp[i][j] = dp[i][j-2] || ((p.charAt(j-2) == '.' || p.charAt(j-2) == s.charAt(i-1)) && dp[i-1][j]);
break;
default:
dp[i][j] = s.charAt(i-1) == p.charAt(j-1) && dp[i-1][j-1];
}
}
}
return dp[s.length()][p.length()];
}
}