-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathLongestSubstringWithAtMostTwoDistinctCharacters159.java
52 lines (48 loc) · 1.37 KB
/
LongestSubstringWithAtMostTwoDistinctCharacters159.java
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
48
49
50
51
52
/**
* Given a string s , find the length of the longest substring t that contains
* at most 2 distinct characters.
*
* Example 1:
* Input: "eceba"
* Output: 3
* Explanation: t is "ece" which its length is 3.
*
* Example 2:
* Input: "ccaabbb"
* Output: 5
* Explanation: t is "aabbb" which its length is 5.
*/
public class LongestSubstringWithAtMostTwoDistinctCharacters159 {
public int lengthOfLongestSubstringTwoDistinct(String s) {
return lengthOfLongestSubstringKDistinct(s, 2);
}
// 340. Longest Substring with At Most K Distinct Characters
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s == null || s.length() == 0 || k == 0) return 0;
int[] map = new int[256];
int left = 0;
int right = 0;
int N = s.length();
char[] chars = s.toCharArray();
int num = 0;
int res = 0;
while (right < N) {
char c = chars[right++];
if (map[c] == 0) {
num++;
}
map[c]++;
while (num > k) {
char leftC = chars[left++];
if (map[leftC] == 1) {
num--;
}
map[leftC]--;
}
if (right - left > res) {
res = right - left;
}
}
return res;
}
}