-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlongestSubstringTwoDistinct.java
More file actions
37 lines (36 loc) · 1.34 KB
/
longestSubstringTwoDistinct.java
File metadata and controls
37 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class longestSubstringTwoDistinct {
public static void main(String[] args){
longestSubstringTwoDistinct obj = new longestSubstringTwoDistinct();
String s = "ecebaaaaae";
System.out.println(obj.longest(s));
}
/* Given a string s, find the length of the longest substring t that contains at most 2 distinct characters
注意是 "AT MOST",比"EXACTLY EQUAL"简单一些
* */
private int longest(String s){
int res = 0;
if(s == null || s.length() == 0){
return 0;
}
Map<Character, Integer> map = new HashMap<>();
int count = 0; // how many distinct characters in substring
for(int left = 0, right = 0; right < s.length(); right ++){
map.putIfAbsent(s.charAt(right), 0);
if(map.get(s.charAt(right)) == 0){
count ++;
}
map.put(s.charAt(right),map.get(s.charAt(right))+1);
while(left <= right && count > 2){
map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
if(map.get(s.charAt(left)) == 0){
count --;
}
left ++;
}
System.out.println(s.substring(left, right+1));
res = Math.max(res, right-left+1);
}
return res;
}
}