-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringAtmostTwoDistinctM2.java
More file actions
38 lines (33 loc) · 1.1 KB
/
LongestSubstringAtmostTwoDistinctM2.java
File metadata and controls
38 lines (33 loc) · 1.1 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
/**
* Leetcode problem #159, Longest Subsstring with Atmost Two Distinctive
* Characters
* https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
* Moving start index on by one, till ot the next start.
*/
public class LongestSubstringAtmostTwoDistinctM2 {
public static void main(String[] args) {
String s = "eceba";
System.out.println(lengthOfLongestSubstringTwoDistinct(s));
}
public static int lengthOfLongestSubstringTwoDistinct(String s) {
int start = 0, len = 0, distinct = 0;
int[] charArr = new int[256];
for (int i = 0; i < s.length(); i++) {
int key = s.charAt(i);
if (charArr[key] == 0) {
distinct++;
}
charArr[key]++;
while (distinct == 3) {
int startKey = s.charAt(start);
charArr[startKey]--;
start++;
if (charArr[startKey] == 0) {
distinct--;
}
}
len = Math.max(i - start + 1, len);
}
return len;
}
}