-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathRegularExpressionMatch.cpp
62 lines (49 loc) · 1.31 KB
/
RegularExpressionMatch.cpp
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
/*
Implement wildcard pattern matching with support for '?' and '*'.
'?' : Matches any single character.
'*' : Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
int isMatch(const char *s, const char *p)
Examples :
isMatch("aa","a") → 0
isMatch("aa","aa") → 1
isMatch("aaa","aa") → 0
isMatch("aa", "*") → 1
isMatch("aa", "a*") → 1
isMatch("ab", "?*") → 1
isMatch("aab", "c*a*b") → 0
Return 1/0 for this problem.
LINK: https://www.interviewbit.com/problems/regular-expression-match/
*/
int Solution::isMatch(const string A, const string B)
{
int na = A.length();
int nb = B.length();
if(nb==0)
return (na==0);
bool dp[nb+1];
memset(dp,false,sizeof(dp));
dp[0]=true;
for(int i=1;i<=nb;i++)
if(B[i-1]=='*')
dp[i] = dp[i-1];
bool temp;
for(int i=1;i<=na;i++)
{
temp = dp[0];
for(int j=1;j<=nb;j++)
{
bool match = false;
if(B[j-1]=='?' || A[i-1]==B[j-1])
match = temp;
else
if(B[j-1]=='*')
match = dp[j] || dp[j-1];
temp = dp[j];
dp[j] = match;
}
dp[0]=false;
}
return dp[nb];
}