-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegular_Expression_Matching.cpp
More file actions
87 lines (80 loc) · 1.41 KB
/
Copy pathRegular_Expression_Matching.cpp
File metadata and controls
87 lines (80 loc) · 1.41 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
//#include "iostream"
#include "stdio.h"
#include "string"
#include "string.h"
using namespace std;
class Solution
{
public:
bool isMatch(string s, string p)
{
return helper( s, p, 0, 0);
}
bool helper(string s, string p, int i, int j)
{
if (0 == p[j])
{
return 0 == s[i];
}
if ('*' != p[j + 1])
{
if (s[i] == p[j] || '.' == p[j] && s[i] != 0 )
{
return helper( s, p, i + 1, j + 1 );
}
return false;
}
else
{
while(s[i] == p[j] || '.' == p[j] && s[i] != 0 )
{
if (helper( s, p, i, j + 2))
{
return true;
}
i++;
}
return helper( s, p, i, j + 2);
}
}
};
bool isMatch(const char *s, const char *p)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( 0 == *p) return 0 == *s;
if(*(p+1) != '*')
{
if(*p == *s || (*p) == '.' && (*s) != 0)
{
return isMatch(s+1, p+1);
}
return false;
}
else
{
while(*p == *s || ((*p) == '.' && (*s) != 0))
{
if(isMatch(s, p + 2))
{
return true;
}
s++;
}
return isMatch(s, p + 2);
}
}
int main(int argc, char const *argv[])
{
Solution sol1;
if (sol1.isMatch("aa", "a" ))
{
printf("true\n");
}
else
{
printf("false\n");
}
printf("hello\n");
return 0;
}