-
Notifications
You must be signed in to change notification settings - Fork 2
/
LongestSubstringWithAtMostTwoDistinctCharacters.java
59 lines (51 loc) · 1.77 KB
/
LongestSubstringWithAtMostTwoDistinctCharacters.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
53
54
55
56
57
58
59
package Leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* @author kalpak
*
* 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 LongestSubstringWithAtMostTwoDistinctCharacters {
public static int lengthOfLongestSubstringTwoDistinct(String s) {
if(s.length() == 0)
return 0;
Map<Character, Integer> map = new HashMap<>();
int result = Integer.MIN_VALUE;
int i =0;
int j = 0;
while(j < s.length()) {
map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1); // enter the characters into the map
if(map.size() <= 2) { // since the problem asks from atmost two distinct characters
result = Math.max(result, j - i + 1);
} else if(map.size() > 2) { // if more than 2 distinct characters, slide the window
while(map.size() > 2) { // remove characters from the left side of the window
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
if(map.get(s.charAt(i)) == 0) // remove its entry from map when the character count is 0
map.remove(s.charAt(i));
i++;
}
}
j++;
}
return result;
}
public static void main(String[] args) {
String str1= "eceba";
System.out.println(lengthOfLongestSubstringTwoDistinct(str1));
str1 = "ccaabbb";
System.out.println(lengthOfLongestSubstringTwoDistinct(str1));
}
}