-
Notifications
You must be signed in to change notification settings - Fork 0
/
LengthOfLongestSubstringTwoDistinct.java
30 lines (28 loc) · 1.41 KB
/
LengthOfLongestSubstringTwoDistinct.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
class LengthOfLongestSubstringTwoDistinct {
public int lengthOfLongestSubstringTwoDistinct(String str) {
/*
Runtime: 50 ms, faster than 81.07% of Java online submissions for Longest Substring with At Most Two Distinct Characters.
Memory Usage: 43.4 MB, less than 85.49% of Java online submissions for Longest Substring with At Most Two Distinct Characters.
*/
if (str == null || str.length() == 0)
throw new IllegalArgumentException();
int windowStart = 0, maxLength = 0;
Map<Character, Integer> charFrequencyMap = new HashMap<>();
// in the following loop we'll try to extend the range [windowStart, windowEnd]
for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
char rightChar = str.charAt(windowEnd);
charFrequencyMap.put(rightChar, charFrequencyMap.getOrDefault(rightChar, 0) + 1);
// shrink the sliding window, until we are left with 'k' distinct characters in the frequency map
while (charFrequencyMap.size() > 2) {
char leftChar = str.charAt(windowStart);
charFrequencyMap.put(leftChar, charFrequencyMap.get(leftChar) - 1);
if (charFrequencyMap.get(leftChar) == 0) {
charFrequencyMap.remove(leftChar);
}
windowStart++; // shrink the window
}
maxLength = Math.max(maxLength, windowEnd - windowStart + 1); // remember the maximum length so far
}
return maxLength;
}
}