-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregularExpressionMatch.java
More file actions
55 lines (51 loc) · 1.56 KB
/
regularExpressionMatch.java
File metadata and controls
55 lines (51 loc) · 1.56 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
46
47
48
49
50
51
52
53
54
55
public class Solution {
public boolean isMatch(String s, String p) {
// Start typing your Java solution below
// DO NOT write main() function
if(s.length() == 0 && p.length() == 0) return true;
int len1 = s.length();
int len2 = p.length();
int i = 0;
int j = 0;
while(i < len1 && j < len2) {
char c1 = s.charAt(i);
char curr = p.charAt(j);
char next = ' ';
if(j + 1 < len2) {
next = p.charAt(j + 1);
}
if(c1 == curr || curr == '.') {
if(next != '*') {
++i;
++j;
}
else {
if(isMatch(s.substring(i + 1), p.substring(j))) {
return true;
} else {
if(j >= p.length()) {
return false;
} else {
j += 2;
}
}
}
} else {
if(next == '*') {
j += 2;
} else {
return false;
}
}
}
if(i < len1) return false;
while(j < len2) {
char curr = p.charAt(j);
if(j + 1 >= p.length()) return false;
char next = p.charAt(j + 1);
if(next != '*') return false;
j += 2;
}
return true;
}
}