forked from dimitar9/Algorithm_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest159.java
More file actions
47 lines (43 loc) · 1.37 KB
/
Test159.java
File metadata and controls
47 lines (43 loc) · 1.37 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
38
39
40
41
42
43
44
45
46
47
import java.util.HashMap;
import java.util.Hashtable;
public class Test159 {
public static int lengthOfLongestSubstringTwoDistinct(String s) {
Hashtable<Character,Integer> hm = new Hashtable<Character,Integer>();
int len = s.length();
int j = 0;
int maxlen = 0;
int curDistinctCount=0;
for(int i =0; i < len; i++){
if(hm.get(s.charAt(i))==null || hm.get(s.charAt(i))==0){
hm.put(s.charAt(i), 1);
curDistinctCount++;
} else {
hm.put(s.charAt(i), hm.get(s.charAt(i))+1);
}
if(curDistinctCount<=2){
maxlen = Math.max(maxlen, i-j+1);
} else {
while(j<s.length() && hm.get(s.charAt(j))!=null && hm.get(s.charAt(j))!=1){
hm.put(s.charAt(j),hm.get(s.charAt(j))-1);
j++;
}
if(j>=len) break;
hm.put(s.charAt(j),0);
curDistinctCount--;
j++;
}
}
return maxlen;
}
static int sum(int a, int b){
int c = a + b;
return c;
}
public static void main(String[] args) {
String s = "aaabc";
//int ret =lengthOfLongestSubstringTwoDistinct(s);
int result = sum(1,3);
System.out.println(result);
//System.out.println(ret);
}
}