-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegularExpressionMatching.java
56 lines (53 loc) · 1.61 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
/**
* Created by cpacm on 2017/1/13.
*/
public class RegularExpressionMatching {
public static void main(String[] args) {
System.out.println(isMatch("", ".*"));
}
public static boolean isMatch(String s, String p) {
return match(s, p, 0, 0);
}
public static boolean match(String s, String p, int sIndex, int pIndex) {
if (sIndex == s.length() && pIndex == p.length()) {
return true;
}
if (pIndex >= p.length()) {
return false;
}
int pPlus = 0;
char pChar = p.charAt(pIndex);
if (pIndex + 1 < p.length() && p.charAt(pIndex + 1) == '*') {
pPlus = 1;
}
if (sIndex >= s.length()) {
if (pPlus == 1) {
return match(s, p, sIndex, pIndex + 2);
} else {
return false;
}
}
char sChar = s.charAt(sIndex);
if (pPlus == 0) {
if (sChar == pChar || pChar == '.') {
return match(s, p, sIndex + 1, pIndex + 1);
} else {
return false;
}
} else {
if (sChar == pChar || pChar == '.') {
if (!match(s, p, sIndex + 1, pIndex + 2)) {
if (!match(s, p, sIndex + 1, pIndex)) {
return match(s, p, sIndex, pIndex + 2);
} else {
return true;
}
} else {
return true;
}
} else {
return match(s, p, sIndex, pIndex + 2);
}
}
}
}