-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular_expression_matching.cpp
More file actions
60 lines (56 loc) · 1.53 KB
/
regular_expression_matching.cpp
File metadata and controls
60 lines (56 loc) · 1.53 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
/*
* =====================================================================================
*
* Filename: regular_expression_matching.cpp
*
* Description: Regular Expression Matching.
* Given an input string (s) and a pattern (p), implement regular
* expression matching with support for '.' and '*'.
*
* Version: 1.0
* Created: 03/15/19 11:20:21
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
class Solution
{
public:
bool isMatch(const std::string& s, const std::string& p)
{
if (p.empty())
{
return s.empty();
}
bool first_match = !s.empty() && (s[0] == p[0] || p[0] == '.');
if (p.length() > 1 && p[1] == '*')
{
return isMatch(s, p.substr(2)) || (first_match && isMatch(s.substr(1), p));
}
else
{
return first_match && isMatch(s.substr(1), p.substr(1));
}
}
};
int main(int argc, char* argv[])
{
std::string s = "aab";
std::string p = "c*a*b";
if (argc > 2)
{
s = argv[1];
p = argv[2];
}
auto matched = Solution().isMatch(s, p);
printf("Input string: `%s`, pattern: `%s`\n", s.c_str(), p.c_str());
printf("Is matched? %s\n", (matched ? "Yes" : "No"));
return 0;
}