From aa6da656d3ad91c6f624af0ab5b0034cf83c2a34 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Fri, 14 Apr 2023 04:41:43 +0800 Subject: [PATCH] add: 230414-CN --- cpp/2304/230414-CN.cpp | 70 +++++++++++++++++++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 cpp/2304/230414-CN.cpp diff --git a/cpp/2304/230414-CN.cpp b/cpp/2304/230414-CN.cpp new file mode 100644 index 0000000..1adecaf --- /dev/null +++ b/cpp/2304/230414-CN.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +/** + * 1023. Camelcase Matching + * + * Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise. + * A query word queries[i] matches pattern if you can insert lowercase English letters pattern so that it equals the query. You may insert each character at any position and you may not insert any characters. + * + * Note: this solution (using regex) is VERY VERY slow + */ + +class Solution { + template + using V = std::vector; + using S = std::string; + static constexpr inline bool is_lower(char c) { return c >= 'a' && c <= 'z'; } + static V camelMatchAlt(const V&, const S&); +public: + static V camelMatch(const V&, const S&); +}; + +std::vector Solution::camelMatch(const V& queries, const S& pattern) { + // return camelMatchAlt(queries, pattern); + S regex_str = "[a-z]*"; + for (char ch : pattern) { + regex_str += ch; + regex_str += "[a-z]*"; + } + std::regex matcher(regex_str, std::regex_constants::optimize); + std::vector ret; + for (auto&& q : queries) + ret.push_back(std::regex_match(q, matcher)); + return ret; +} + +std::vector Solution::camelMatchAlt(const V& qs, const S& p) { + std::vector ret; + const int n = p.length(); + for (auto&& q : qs) { + int pp = 0, pq = 0; + const int m = q.length(); + for (; pq < m; ++pq) + if (q[pq] == p[pp]) { + if (++pp >= n) break; + } else if (!std::islower(q[pq])) break; + ret.push_back(pp >= n && std::all_of(q.begin() + ++pq, q.end(), [](char c) { + return std::islower(c); + })); + } + return ret; +} + +template +std::ostream& operator<<(std::ostream& os, std::vector vec) { + for (auto&& x : vec) + os << x << std::endl; + return os; +} + +int main() { + std::cout << Solution::camelMatch( + {"FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"}, + "FB" + ); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index d1cefb5..7400f38 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230413.cpp) +ADD_EXECUTABLE(2304 230414-CN.cpp)