public String longestNiceSubstring(String s) {
int n = s.length();
int begin = 0;//最长子串开始
int end = 0;//最长子串结束
//枚举子串
for (int i = 0; i < n; ++i) {
int lower = 0;//二进制记录字母大小写出现结果
int upper = 0;
for (int j = i; j < n; ++j) {
if (Character.isLowerCase(s.charAt(j))) {
lower |= 1 << (s.charAt(j) - 'a');
} else {
upper |= 1 << (s.charAt(j) - 'A');
}
//两者相等表示大小写都有
if (lower == upper && j - i > end-begin) {
begin = i;
end = j;
}
}
}
if(begin-end==0)//找不到
return "";
return s.substring(begin, end+1);
}
2022.2.1-----leetcode.1763
于 2022-02-01 19:56:31 首次发布