-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path010_RegularExpressionMatching10.java
162 lines (136 loc) · 5.47 KB
/
010_RegularExpressionMatching10.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Implement regular expression matching with support for '.' and '*'.
*
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
* The matching should cover the entire input string (not partial).
*
* The function prototype should be:
* bool isMatch(const char *s, const char *p)
*
* Some examples:
* isMatch("aa","a") → false
* isMatch("aa","aa") → true
* isMatch("aaa","aa") → false
* isMatch("aa", "a*") → true
* isMatch("aa", ".*") → true
* isMatch("ab", ".*") → true
* isMatch("aab", "c*a*b") → true
*
*/
class Solution {
public boolean isMatch(String s, String p) {
HashMap<String, HashMap<String, Boolean>> cache = new HashMap<>();
return isMatchHelper(s,p,cache);
}
public boolean isMatchHelper(String s, String p, HashMap<String, HashMap<String, Boolean>> cache){
if(cache.containsKey(s)){
if(cache.get(s).containsKey(p)){
return cache.get(s).get(p);
}
}
if(s.length()==0 && p.length()==0) return true;
if(s.length()==0 || p.length()==0){
if(p.length()==0 || (p.length() != 0 && !p.contains("*"))) return false;
}
boolean foundMatch = false;
if(s.length()==0){
if(p.length()>1 && p.charAt(1)=='*') foundMatch = foundMatch || isMatchHelper(s,p.substring(2), cache);
}
else{
if(s.charAt(0)==p.charAt(0) || p.charAt(0)=='.'){
foundMatch = foundMatch || isMatchHelper(s.substring(1), p.substring(1), cache);
}
if(p.length() > 1 && p.charAt(1)=='*'){
if(s.charAt(0)==p.charAt(0) || p.charAt(0)=='.'){
if(p.length()>1) {
foundMatch = foundMatch || isMatchHelper(s.substring(1), p.substring(2), cache);
foundMatch = foundMatch || isMatchHelper(s, p.substring(2), cache);
}
foundMatch = foundMatch || isMatchHelper(s.substring(1), p.substring(1), cache);
foundMatch = foundMatch || isMatchHelper(s.substring(1), p, cache);
}else{
if(p.length()>1) foundMatch = foundMatch || isMatchHelper(s, p.substring(2), cache);
}
}
}
if(cache.containsKey(s)){
HashMap<String, Boolean> incache = cache.get(s);
incache.put(p,foundMatch);
}else{
HashMap<String, Boolean> incache = new HashMap<>();
incache.put(p,foundMatch);
cache.put(s,incache);
}
return foundMatch;
}
}
***************************************************************************************************
class Solution {
int n, m;
int[][] memo;
public boolean isMatch(String s, String p) {
if(s.equals(p)) return true;
n = s.length();
m = p.length();
memo = new int[n + 1][m + 1];
return match(0, 0, s, p);
}
public boolean match(int i, int j, String s, String p) {
if(j == m) return i == n;
if(i < n && j < m && memo[i][j] != 0) return memo[i][j] == 1;
boolean ans;
boolean firstMatch = (i < n && s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');
if(j < m - 1 && p.charAt(j + 1) == '*') ans = match(i, j + 2, s, p) || (firstMatch && i < n && match(i + 1, j, s, p));
else ans = firstMatch && match(i + 1, j + 1, s, p);
if(i < n && j < m)
memo[i][j] = ans?1:-1;
return ans;
}
}
******************************************************************************************
class Solution {
public boolean isMatch(String s, String p) {
if(p.length()==0) return s.length()==0; // base case are we finished with the letters?
if(p.length()>1 && p.charAt(1)=='*'){
if(isMatch(s,p.substring(2))){ //0 repeat
return true;
}
//1 or more than 1 repeat
if(s.length()>0 && (p.charAt(0)=='.' || s.charAt(0)==p.charAt(0)))
return isMatch(s.substring(1),p);
}
else{
if(s.length()>0 && (p.charAt(0)=='.' || s.charAt(0)==p.charAt(0)))
return isMatch(s.substring(1),p.substring(1));
}
return false;
}
}
****************************************************************
class Solution {
Map<String,Boolean> map = new HashMap<>();
public boolean isMatch(String s, String p) {
if(p.length()==0) return s.length()==0; // base case are we finished with the letters in both the strings?
String key = s+"#"+p;
if(map.containsKey(key)) return map.get(key);
Boolean result=false;
if(p.length()>1 && p.charAt(1)=='*'){
if(isMatch(s,p.substring(2))){ //0 repeat
result = true;
map.put(key,result);
return result;
}
//1 or more than 1 repeat
if(s.length()>0 && (p.charAt(0)=='.' || s.charAt(0)==p.charAt(0)))
result = isMatch(s.substring(1),p);
}
else{
if(s.length()>0 && (p.charAt(0)=='.' || s.charAt(0)==p.charAt(0)))
result = isMatch(s.substring(1),p.substring(1)); //both match
}
map.put(key,result);
return result;
}
}