forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_340.java
68 lines (60 loc) · 2.09 KB
/
_340.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
60
61
62
63
64
65
66
67
68
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
/**
* 340. Longest Substring with At Most K Distinct Characters
*
* Given a string, find the length of the longest substring T that contains at most k distinct characters.
For example, Given s = “eceba” and k = 2,
T is "ece" which its length is 3.
*/
public class _340 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/41671/15-lines-java-solution-using-slide-window
*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int[] count = new int[256];
int left = 0;
int result = 0;
int num = 0;
for (int right = 0; right < s.length(); right++) {
if (count[s.charAt(right)]++ == 0) {
num++;
}
if (num > k) {
while (--count[s.charAt(left++)] > 0) {
}
;
num--;
}
result = Math.max(result, right - left + 1);
}
return result;
}
}
public static class Solution2 {
/**This is a more generic solution for any characters.*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
Map<Character, Integer> map = new HashMap<>();
int longest = 0;
int left = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
map.put(c, map.getOrDefault(c, 0) + 1);
while (map.size() > k) {
char leftChar = s.charAt(left);
if (map.containsKey(leftChar)) {
map.put(leftChar, map.get(leftChar) - 1);
if (map.get(leftChar) == 0) {
map.remove(leftChar);
}
}
left++;
}
longest = Math.max(longest, i - left + 1);
}
return longest;
}
}
}