-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_159.java
More file actions
32 lines (25 loc) · 870 Bytes
/
Copy pathleetCode_159.java
File metadata and controls
32 lines (25 loc) · 870 Bytes
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
class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
Set<Character> set = new HashSet<>();
Map<Character, Integer> map = new HashMap<>();
int left = 0 , res =0 ;
for(int right=0; right<s.length(); right++){
map.put(s.charAt(right), right);
set.add(s.charAt(right));
while(set.size() > 2){
char r = '9';
int ii = s.length();
for(char c: set){
if(map.get(c) < ii){
r = c;
ii= map.get(c);
}
}
set.remove(r);
left= ii+1;
}
res = Math.max(res, right - left+1);
}
return res ;
}
}