diff --git a/CMakeLists.txt b/CMakeLists.txt index 440c4cab..4726c14b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -509,6 +509,7 @@ add_task(reconstruct-itinerary) add_task(recover-a-tree-from-preorder-traversal) add_task(recover-binary-search-tree) add_task(reducing-dishes) +add_task(regular-expression-matching) add_task(remove-colored-pieces-if-both-neighbors-are-the-same-color) add_task(remove-duplicate-letters) add_task(remove-duplicates-from-sorted-array) diff --git a/solutions/regular-expression-matching/CMakeLists.txt b/solutions/regular-expression-matching/CMakeLists.txt new file mode 100644 index 00000000..762ced8b --- /dev/null +++ b/solutions/regular-expression-matching/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_regular_expression_matching test.cpp) diff --git a/solutions/regular-expression-matching/solution.hpp b/solutions/regular-expression-matching/solution.hpp new file mode 100644 index 00000000..dfaa806b --- /dev/null +++ b/solutions/regular-expression-matching/solution.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +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(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]; + } +}; diff --git a/solutions/regular-expression-matching/test.cpp b/solutions/regular-expression-matching/test.cpp new file mode 100644 index 00000000..af4058f5 --- /dev/null +++ b/solutions/regular-expression-matching/test.cpp @@ -0,0 +1,39 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::string s; + std::string p; + bool expected; + }; + + std::vector 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); + } +} diff --git a/solutions/valid-number/test.cpp b/solutions/valid-number/test.cpp index fa5dd877..90996130 100644 --- a/solutions/valid-number/test.cpp +++ b/solutions/valid-number/test.cpp @@ -11,7 +11,7 @@ TEST_CASE("VALID NUMBERS") { } } -TEST_CASE("Not Valid Numbers") { +TEST_CASE("NOT VALID NUMBERS") { std::vector not_numbers{"abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53", ".", "e"}; for (const auto ¬_number : not_numbers) {