Skip to content

Commit dd8aadd

Browse files
committed
10
1 parent b826f40 commit dd8aadd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

10_regular_expression_matching.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "test.h"
2+
3+
class Solution
4+
{
5+
public:
6+
bool isMatch(string s, string p)
7+
{
8+
return match(s, 0, p, 0);
9+
}
10+
11+
bool match(const string& s, unsigned int i, const string& p, unsigned int j)
12+
{
13+
const char eof = 0;
14+
char s1 = i < s.length() ? s[i] : eof;
15+
char p1 = j < p.length() ? p[j] : eof;
16+
char p2 = j + 1 < p.length() ? p[j + 1] : eof;
17+
18+
if (s1 == eof)
19+
{
20+
if (p1 == eof)
21+
return true;
22+
23+
if (p2 == '*')
24+
return match(s, i, p, j + 2);
25+
26+
return false;
27+
}
28+
29+
if (p1 == s1 || p1 == '.')
30+
{
31+
return p2 == '*' ? (match(s, i + 1, p, j) || match(s, i, p, j + 2)): match(s, i + 1, p, j + 1);
32+
}
33+
else
34+
{
35+
return p2 == '*' ? match(s, i, p, j + 2) : false;
36+
}
37+
38+
return true;
39+
}
40+
};
41+
42+
43+
int main()
44+
{
45+
Solution s;
46+
cout
47+
<< s.isMatch("", "c*c*") << " "
48+
<< s.isMatch("aaa", "a*a") << " "
49+
<< s.isMatch("abc", "ab.*") << " "
50+
<< s.isMatch("abc", "ab.*") << " "
51+
<< s.isMatch("abccccc", "abc.*") << " "
52+
<< s.isMatch("abccccc", "abc.*d") << " "
53+
<< endl;
54+
55+
}

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ targets = \
88
7_reverse_interger.exec\
99
8_string_to_interger.exec\
1010
9_palindrome_number.exec\
11+
10_regular_expression_matching.exec\
1112

1213

1314
CC = g++

0 commit comments

Comments
 (0)