-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.regular-expression-matching.cpp
More file actions
46 lines (40 loc) · 1.21 KB
/
10.regular-expression-matching.cpp
File metadata and controls
46 lines (40 loc) · 1.21 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
#include "testharness.h"
#include <stack>
#include <set>
#include <iostream>
using namespace std;
bool isMatch(char *s, char *p) {
if (*s == '\0') {
while (*p != '\0') {
if (*(p+1) != '*') return false;
p += 2;
}
return true;
}
if (*(p+1) == '*') {
while (*s != '\0' && (*p == *s || *p == '.')) {
if (isMatch(s++, p + 2)) return true;
}
return isMatch(s, p + 2);
}
return (*p == *s || *p == '.') ? isMatch(s+1, p+1) : false;
}
class Solution {
public:
bool isMatch(const string& s, const string& p) {
return ::isMatch((char*)s.c_str(), (char*)p.c_str());
}
};
TEST(Solution, test) {
ASSERT_EQ(true, isMatch("aa", ".*aa"));
ASSERT_EQ(true, isMatch("aa","aa"));
ASSERT_EQ(false, isMatch("aaa","aa"));
ASSERT_EQ(false, isMatch("aa","aaaa"));
ASSERT_EQ(true, isMatch("aa", "a*"));
ASSERT_EQ(true, isMatch("aa", ".*"));
ASSERT_EQ(true, isMatch("ab", ".*"));
ASSERT_EQ(true, isMatch("aab", "c*a*b"));
ASSERT_EQ(true, isMatch("aaab", "a*a*c*aaab"));
ASSERT_EQ(true, isMatch("aaab", "a*a*c*.aab"));
ASSERT_EQ(false, isMatch("acaabbaccbbacaabbbb", "a*.*b*.*a*aa*a*"));
}