给定一个字符串,求其中最长子串,该子串最多有两个不同的字符
public static int longestSubstringOnlyTwoDistinctChar(String s) {
if(s==null || s.length()==0) return 9;
Map<Character,Integer> map = new HashMap<>();
int res = 0 ;
int start = 0 ;
for(int i =0 ; i < s.length() ;) {
char cur = s.charAt(i);
if(map.isEmpty()) {
map.put(cur, 1);
}else if(!map.containsKey(cur)) {
if(map.entrySet().size()==2) {
res = res>(i-start)?res:(i-start);
map.remove(s.charAt(start));
int temp = start;
while(s.charAt(temp)==s.charAt(start)) temp++;
start = temp;
}
map.put(cur, 1);
}else if(map.get(cur)!=null) {
map.replace(cur, map.get(cur)+1);
}
i++;
}
return res;
}