-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.regular-expression-matching.cpp
197 lines (184 loc) · 4.89 KB
/
10.regular-expression-matching.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* @lc app=leetcode id=10 lang=cpp
*
* [10] Regular Expression Matching
*
* https://leetcode.com/problems/regular-expression-matching/description/
*
* algorithms
* Hard (28.17%)
* Likes: 7566
* Dislikes: 1086
* Total Accepted: 644.9K
* Total Submissions: 2.3M
* Testcase Example: '"aa"\n"a"'
*
* Given an input string s and a pattern p, implement regular expression
* matching with support for '.' and '*' where:
*
*
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
*
* The matching should cover the entire input string (not partial).
*
*
* Example 1:
*
*
* Input: s = "aa", p = "a"
* Output: false
* Explanation: "a" does not match the entire string "aa".
*
*
* Example 2:
*
*
* Input: s = "aa", p = "a*"
* Output: true
* Explanation: '*' means zero or more of the preceding element, 'a'.
* Therefore, by repeating 'a' once, it becomes "aa".
*
*
* Example 3:
*
*
* Input: s = "ab", p = ".*"
* Output: true
* Explanation: ".*" means "zero or more (*) of any character (.)".
*
*
*
* Constraints:
*
*
* 1 <= s.length <= 20
* 1 <= p.length <= 30
* s contains only lowercase English letters.
* p contains only lowercase English letters, '.', and '*'.
* It is guaranteed for each appearance of the character '*', there will be a
* previous valid character to match.
*
*
*/
// @lc code=start
/*
* @lc app=leetcode id=10 lang=cpp
*
* [10] Regular Expression Matching
*/
// @lc code=start
#if 1
class Solution {
public:
bool isMatch(string s, string p)
{
if(p.empty())
return s.empty();
bool matchFirstChar = !s.empty() && (s[0] == p[0] || p[0] == '.');
if(p.size() >= 2 && p[1] == '*')
{
return (matchFirstChar && !s.empty() && isMatch(s.substr(1), p)) || (isMatch(s, p.substr(2)));
}
else
{
return matchFirstChar && !s.empty() && isMatch(s.substr(1), p.substr(1));
}
}
};
#endif
#if 0 // first try: All accepted but time limit excceeded
class Solution {
public:
bool isMatch(string s, string p) {
int sIndex = 0, pIndex = 0;
while(sIndex < s.size() && pIndex < p.size())
{
bool matchAnyChar = p[pIndex] == '.';
bool matchAnyLength = pIndex + 1 < p.size() && p[pIndex + 1] == '*';
bool hasMatchedAnyLength = true;
if(matchAnyLength)
{
if(pIndex + 2 < p.size())
{
if(matchAnyChar)
{
int newSIndex = sIndex;
while(newSIndex < s.size())
{
// recursive:
if(isMatch(string(s.begin()+newSIndex, s.end()), string(p.begin()+pIndex+2,p.end())))
{
return true;
}
++newSIndex;
}
hasMatchedAnyLength = true;
sIndex = newSIndex;
}
else
{
int newSIndex = sIndex;
bool sameLetter = true;
while(newSIndex < s.size() && sameLetter)
{
// recursive:
if(isMatch(string(s.begin()+newSIndex, s.end()), string(p.begin()+pIndex+2,p.end())))
{
return true;
}
sameLetter = s[newSIndex] == p[pIndex];
++newSIndex;
}
hasMatchedAnyLength = s[sIndex] == p[pIndex];
}
}
else
{
while(sIndex < s.size() && (s[sIndex] == p[pIndex] || matchAnyChar))
{
++sIndex;
}
--sIndex;
}
}
else if(!matchAnyChar && s[sIndex] != p[pIndex])
{
return false;
}
if(hasMatchedAnyLength)
++sIndex;
++pIndex;
if(matchAnyLength)
++pIndex;
}
while(sIndex >= s.size() && pIndex < p.size())
{
if(pIndex + 1 >= p.size() || p[pIndex + 1] != '*')
{
return false;
}
pIndex += 2;
}
return sIndex >= s.size() && pIndex >= p.size();
}
};
#endif
/*
"aaa"
"a.*.*a"
"aacccabaaba"
"a.*.*aba"
"mississippi"
"mis*is*p*."
"a"
"ab*"
"aabcbcbcaccbcaabc"
".*a*aa*.*b*.c*.*a*"
"abcaaaaaaabaabcabac"
".*ab.a.*a*a*.*b*b*"
"bcccccbaccccacaa"
".*bb*c*a*b*.*b*b*c*"
*/
// @lc code=end