FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DSA_Problems/LeetCode/0010_RegularExpressionMatching.cpp at main · lakshitcodes/DSA_Problems · GitHub
lakshitcodes
/
DSA_Problems
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DSA_Problems
/
LeetCode
/
0010_RegularExpressionMatching.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (23 loc) · 1.15 KB
Breadcrumbs
DSA_Problems
/
LeetCode
/
0010_RegularExpressionMatching.cpp
Copy path
File metadata and controls
30 lines (23 loc) · 1.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
bits/stdc++.h
>
using
namespace
std
;
//
Question Link : https://leetcode.com/problems/regular-expression-matching/
class
Solution
{
public:
vector<vector<
int
>> dp;
bool
solve
(
int
i,
int
j, string& s, string& p) {
if
(j == p.
length
())
return
i == s.
length
();
//
If pattern is exhausted, check if string is also exhausted
if
(dp[i][j] != -
1
)
return
dp[i][j];
//
Memoization
bool
first_match = (i < s.
length
() && (s[i] == p[j] || p[j] ==
'
.
'
));
if
(j +
1
< p.
length
() && p[j +
1
] ==
'
*
'
) {
//
Case 1: Ignore '*' and preceding character (match zero times)
//
Case 2: Use '*' to match more characters (only if first_match is true)
return
dp[i][j] = (
solve
(i, j +
2
, s, p) || (first_match &&
solve
(i +
1
, j, s, p)));
}
else
{
return
dp[i][j] = (first_match &&
solve
(i +
1
, j +
1
, s, p));
}
}
bool
isMatch
(string s, string p) {
dp = vector<vector<
int
>>(s.
length
() +
1
, vector<
int
>(p.
length
() +
1
, -
1
));
return
solve
(
0
,
0
, s, p);
}
};
Back
|
FazBrowse Home
|
New Git URL