-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.java
More file actions
45 lines (40 loc) · 1.38 KB
/
RegularExpressionMatching.java
File metadata and controls
45 lines (40 loc) · 1.38 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
45
public class RegularExpressionMatching {
public static boolean isMatch(String s, String p) {
if (s.length() < 1) {
if (p.length() < 1 || (p.length() == 2 && p.charAt(1) == '*')) return true;
return false;
}
boolean ret = isMatchHelper(s, p, 0, 0);
System.out.println(ret);
return ret;
}
public static boolean isMatchHelper(String s, String p, int si, int pi) {
if (pi >= p.length()) {
System.out.print("`");
if (si >= s.length()) return true;
System.out.println("-");
return false;
}
if (si >= s.length()) {
return false;
}
char sc = s.charAt(si);
char pc = p.charAt(pi);
if (pi == p.length()-1 || p.charAt(pi+1) != '*') {
if (!matchChar(sc, pc)) return false;
return isMatchHelper(s, p, si+1, pi+1);
}
if (isMatchHelper(s, p, si, pi+2)) return true;
while(si+1 < s.length()) {
sc = s.charAt(si + 1);
if (matchChar(sc, pc)) {
if (isMatchHelper(s, p, ++si, pi+2)) return true;
}
}
return isMatchHelper(s, p, ++si, pi+2);
}
public static boolean matchChar(char sc, char pc) {
if (sc == pc || pc == '.') return true;
return false;
}
}