add: 220201-CN

This commit is contained in:
Lam Haoyin 2022-02-01 21:32:08 +08:00
parent b3db615aee
commit 5fd7285248
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 35 additions and 4 deletions

View File

@ -3,4 +3,4 @@ PROJECT(2201)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2201 220131.cpp)
ADD_EXECUTABLE(2201 220129.cpp)

View File

@ -1,8 +1,39 @@
#include <string>
#include <cctype>
#include <cstdio>
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;
}
};
};
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;
}