diff --git a/cpp/2204/220417-CN.cpp b/cpp/2204/220417-CN.cpp new file mode 100644 index 0000000..67c242b --- /dev/null +++ b/cpp/2204/220417-CN.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +class Solution { +public: + static std::string mostCommonWord(std::string paragraph, const std::vector& banned) { + std::unordered_map freq; + std::for_each(paragraph.begin(), paragraph.end(), [](char& c){ c = std::tolower(c); }); + paragraph.push_back('.'); + const char* ptr = paragraph.c_str(); + for (const char* p1 = ptr, * p2 = ptr; *p2; ++p2) { + if (!std::isalpha(*p2)) { + if (p2 - p1) + ++freq[std::string(p1, p2)]; + p1 = 1 + p2; + } + } + std::for_each(banned.cbegin(), banned.cend(), [&freq](const std::string& str) { freq.erase(str); }); + return std::max_element(freq.cbegin(), freq.cend(), [](const std::pair& x, const std::pair& y) { return x.second < y.second; })->first; + } +}; + +int main() { + std::cout << Solution::mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", {"hit"}); +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 36bd6e6..889d024 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220417.cpp) +ADD_EXECUTABLE(2204 220417-CN.cpp)