-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisMatch.cpp
More file actions
executable file
·53 lines (44 loc) · 1.22 KB
/
isMatch.cpp
File metadata and controls
executable file
·53 lines (44 loc) · 1.22 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
/*
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
*/
#include <algorithm>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
bool isMatch(char *s, char *p){
if(*p == '\0')
return (*s == '\0');
// if the next character is '*', we just test it
if(*(p+1) != '*')
return (*p == *s || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
// next is '*',
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[])
{
char s[] = "aabc";
char p[] = ".*";
if (isMatch(s,p))
cout << "Matched" << endl;
else
cout << "Not Matched" << endl;
return 0;
}