原题链接:https://leetcode-cn.com/problems/longest-nice-substring/
class Solution {
public:
string longestNiceSubstring(string s) {
int n = s.size();
for (int l = n; l > 1; --l) {
for (int i = 0; i + l <= n; ++i) {
if (isNiceSubstring(s.substr(i, l))) {
return s.substr(i, l);
}
}
}
return "";
}
bool isNiceSubstring(string str) {
int n = str.size();
int lower = 0, upper = 0;
for (int i = 0; i < n; ++i) {
if (islower(str[i])) {
lower |= 1 << str[i] - 'a';
} else {
upper |= 1 << str[i] - 'A';
}
}
return lower != upper ? false : true;
}
};