-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringAtmostTwoDistinctM1.java
More file actions
37 lines (30 loc) · 1.08 KB
/
LongestSubstringAtmostTwoDistinctM1.java
File metadata and controls
37 lines (30 loc) · 1.08 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
import java.util.Collections;
import java.util.HashMap;
/**
* Leetcode problem #159, Longest Subsstring with Atmost Two Distinctive
* Characters
* https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
* Directly jumping to next start indexing with the help of hashmaps
*/
public class LongestSubstringAtmostTwoDistinctM1 {
public static void main(String[] args) {
String s = "eceba";
System.out.println(lengthOfLongestSubstringTwoDistinct(s));
}
public static int lengthOfLongestSubstringTwoDistinct(String s) {
int start = 0;
int len = 0;
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), i);
if (map.size() == 3) {
// Deleting leftmost character
int left_idx = Collections.min(map.values());
map.remove(s.charAt(left_idx));
start = left_idx + 1;
}
len = Math.max(i - start + 1, len);
}
return len;
}
}