-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.java
More file actions
110 lines (101 loc) · 3.75 KB
/
RegularExpressionMatching.java
File metadata and controls
110 lines (101 loc) · 3.75 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
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/*
* https://leetcode.com/problems/regular-expression-matching/
*/
public class RegularExpressionMatching {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
}
List<String> groups = splitIntoGroups(p);
return isMatch(s, groups);
}
private List<String> splitIntoGroups(String p) {
List<String> groups = new ArrayList<>();
int startIndex = 0;
for (int i = 0; i < p.length(); i++) {
char c = p.charAt(i);
if (c == '*') {
String group1 = p.substring(startIndex, i - 1);
String group2 = p.substring(i - 1, i + 1);
startIndex = i + 1;
groups.add(group1);
groups.add(group2);
} else if (c == '.') {
String group1 = p.substring(startIndex, i);
groups.add(group1);
if (i != p.length() - 1 && p.charAt(i + 1) == '*') {
String group2 = p.substring(i, i + 2);
groups.add(group2);
startIndex = i + 2;
i++;
} else {
String group2 = p.substring(i, i + 1);
groups.add(group2);
startIndex = i + 1;
}
}
}
if (startIndex != p.length()) {
String group = p.substring(startIndex);
groups.add(group);
}
return groups;
}
private boolean isMatch(String s, List<String> groups) {
List<String> filteredGroups = groups.stream().filter(s1 -> !s1.isEmpty()).collect(Collectors.toList());
int[] flags = new int[filteredGroups.size()];
return isMatch(s, filteredGroups, flags, 0);
}
private boolean isMatch(String s, List<String> groups, int[] flags, int currentFlagIndex) {
if (currentFlagIndex == groups.size() - 1) {
return groupMatch(s.substring(flags[currentFlagIndex]), groups.get(currentFlagIndex));
}
if (flags[flags.length - 1] == s.length() + 1) {
return false;
}
int minIndex = flags[currentFlagIndex];
int maxIndex = flags[currentFlagIndex + 1];
String currentString = s.substring(minIndex, maxIndex);
if (groupMatch(currentString, groups.get(currentFlagIndex))) {
if (isMatch(s, groups, flags, currentFlagIndex + 1)) {
return true;
} else {
return isMatch(s, groups, incrementAllFlagsAfterIndex(flags, currentFlagIndex + 1), currentFlagIndex);
}
} else {
return isMatch(s, groups, incrementAllFlagsAfterIndex(flags, currentFlagIndex + 1), currentFlagIndex);
}
}
private boolean groupMatch(String s, String p) {
if (p.equals(".*")) {
return true;
} else if (p.equals(".")) {
return s.length() == 1;
} else if (p.endsWith("*")) {
char starChar = p.charAt(0);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (starChar != c) {
return false;
}
}
return true;
} else {
return p.equals(s);
}
}
private int[] incrementAllFlagsAfterIndex(int[] flags, int index) {
int[] clone = flags.clone();
for (int i = index; i < clone.length; i++) {
clone[i]++;
}
return clone;
}
public static void main(String[] args) {
System.out.println(new RegularExpressionMatching().isMatch("mississippi", "mis*is*.p*.")); // true
}
}