forked from lee2018jian/airbnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegularExpression
73 lines (67 loc) · 2.35 KB
/
RegularExpression
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
public class RegularExpression {
public static 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)=='*') {
// hoop ho*p hop ho*p
return regMatch(source.substring(1),pattern)||regMatch(source,pattern.substring(2));
}else if(pattern.charAt(1)=='+') {
// hoop ho+p hop ho+p
return regMatch(source.substring(1),pattern.substring(2));
}else {
return regMatch(source.substring(1),pattern.substring(1));
}
}
return pattern.charAt(1)=='*'&®Match(source,pattern.substring(2));
}
public static boolean regMatchDP(String source, String pattern) {
int m = source.length(), n = pattern.length();
boolean[][] dp = new boolean[m+1][n+1];
dp[0][0]=true;
for(int i = 1 ; i <= n;i++) {
if(pattern.charAt(i-1)=='*' &&dp[0][i-2]) {
dp[0][i]=dp[0][i-2];
}
}
for(int i = 1 ; i <=m ;i++) {
for(int j =1 ; j <= n ;j++) {
if(pattern.charAt(j-1)=='.'||pattern.charAt(j-1)==source.charAt(i-1)) {
dp[i][j]=dp[i-1][j-1];
}
if(pattern.charAt(j-1)=='*') {
if(pattern.charAt(j-2)!=source.charAt(i-1)&&pattern.charAt(j-2)!='.') {
dp[i][j]=dp[i][j-2];
}else {
dp[i][j]=dp[i][j-1]||dp[i][j-2]||dp[i-1][j];
}
}else if(pattern.charAt(j-1)=='+'){
dp[i][j]=dp[i-1][j-2];
}
}
}
return dp[m][n];
}
public static void main(String[] args) {
System.out.println(regMatch("hoop","hop"));
System.out.println();
System.out.println(regMatch("hoop","ho*p"));
System.out.println(regMatchDP("hoop","ho*p"));
System.out.println();
System.out.println(regMatch("hop","ho*p"));
System.out.println(regMatchDP("hop","ho*p"));
System.out.println();
System.out.println(regMatch("hoop","ho+p"));
System.out.println(regMatchDP("hoop","ho+p"));
System.out.println();
System.out.println(regMatch("hop","ho+p"));
System.out.println(regMatchDP("hop","ho+p"));
System.out.println();
System.out.println(regMatch("hoop","ho*p*"));
System.out.println(regMatchDP("hoop","ho*p*"));
}
}