-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_regular_expression_matching test.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <regex> | ||
#include <string> | ||
|
||
class Solution { | ||
public: | ||
static bool isMatch(std::string s, std::string p) { | ||
const int n = s.size(), m = p.size(); | ||
|
||
std::vector dp(n + 1, std::vector<bool>(m + 1)); | ||
dp[0][0] = true; | ||
|
||
for (int j = 1; j <= m; ++j) { | ||
dp[0][j] = p[j - 1] == '*' && dp[0][j - 2]; | ||
} | ||
|
||
for (int i = 1; i <= n; ++i) { | ||
for (int j = 1; j <= m; ++j) { | ||
if (p[j - 1] == '*') { | ||
if (p[j - 2] == '.' || p[j - 2] == s[i - 1]) { | ||
dp[i][j] = dp[i][j - 2] || dp[i - 1][j]; | ||
} else { | ||
dp[i][j] = dp[i][j - 2]; | ||
} | ||
} else if (p[j - 1] == '.' || p[j - 1] == s[i - 1]) { | ||
dp[i][j] = dp[i - 1][j - 1]; | ||
} | ||
} | ||
} | ||
|
||
return dp[n][m]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::string s; | ||
std::string p; | ||
bool expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.s = "aa", | ||
.p = "a", | ||
.expected = false, | ||
}, | ||
{ | ||
.s = "aa", | ||
.p = "a*", | ||
.expected = true, | ||
}, | ||
{ | ||
.s = "ab", | ||
.p = ".*", | ||
.expected = true, | ||
}, | ||
{ | ||
.s = "aa", | ||
.p = "a*", | ||
.expected = true, | ||
}, | ||
}; | ||
|
||
for (const auto &[s, p, expected] : test_cases) { | ||
const auto actual = Solution::isMatch(s, p); | ||
REQUIRE(expected == actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters