diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 768abbd..a3a4c87 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220131.cpp) +ADD_EXECUTABLE(2201 220129.cpp) diff --git a/2202/220201-CN.cpp b/2202/220201-CN.cpp index 9716994..54214c8 100644 --- a/2202/220201-CN.cpp +++ b/2202/220201-CN.cpp @@ -1,8 +1,39 @@ #include +#include +#include class Solution { +private: + static bool isNice(const std::string& str, int L, int R) { + bool bucket[2][26]{}; + for (int i = L; i < R; ++i) + *(std::isupper(str[i]) ? &bucket[0][str[i] - 'A'] : &bucket[1][str[i] - 'a']) = true; + for (int i = 0; i < 26; ++i) + if ((bucket[0][i] || bucket[1][i]) && !(bucket[0][i] && bucket[1][i])) + return false; + return true; + } public: - std::string longestNiceSubstring(const std::string& s) { - + static std::string longestNiceSubstring(const std::string& s) { + const int n = s.length(); + std::string ret; + int retMax = 0; + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j <= n; ++j) { + if (j - i > retMax && isNice(s, i, j)) { + retMax = j - i; + ret = s.substr(i, j - i); + } + } + } + return ret; } -}; \ No newline at end of file +}; + +int main() { + std::printf("%s\n", Solution::longestNiceSubstring("YazaAay").c_str()); + std::printf("%s\n", Solution::longestNiceSubstring("Bb").c_str()); + std::printf("%s\n", Solution::longestNiceSubstring("c").c_str()); + std::printf("%s\n", Solution::longestNiceSubstring("dDzeE").c_str()); + return 0; +}