Skip to content

Commit af26eac

Browse files
add 340
1 parent af7dc60 commit af26eac

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Your ideas/fixes/algorithms are more than welcome!
214214
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ReverseString.java) | O(n) |O(1) | Easy | String
215215
|343|[Integer Break](https://leetcode.com/problems/integer-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntegerBreak.java)| O(1)|O(1) | Medium| Math
216216
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_341.java)| O(n)|O(n) | Medium| Stack
217+
|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_340.java)| O(n)|O(1) | Hard| Sliding Window
217218
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NestedListWeightSum.java)| O(n)|O(h)) | Easy| DFS
218219
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CountingBits.java)| O(nlogn)|O(h) | Medium|
219220
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_337.java)| O(n)|O(n)| Medium | DP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 340. Longest Substring with At Most K Distinct Characters
5+
*
6+
* Given a string, find the length of the longest substring T that contains at most k distinct characters.
7+
8+
For example, Given s = “eceba” and k = 2,
9+
10+
T is "ece" which its length is 3.
11+
12+
*/
13+
public class _340 {
14+
15+
/**credit: https://discuss.leetcode.com/topic/41671/15-lines-java-solution-using-slide-window*/
16+
public int lengthOfLongestSubstringKDistinct(String s, int k) {
17+
int[] count = new int[256];
18+
int left = 0;
19+
int result = 0;
20+
int num = 0;
21+
for (int right = 0; right < s.length(); right++) {
22+
if (count[s.charAt(right)]++ == 0) {
23+
num++;
24+
}
25+
if (num > k) {
26+
while (--count[s.charAt(left++)] > 0);
27+
num--;
28+
}
29+
result = Math.max(result, right - left + 1);
30+
}
31+
return result;
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._340;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 6/10/17.
11+
*/
12+
public class _340Test {
13+
private static _340 test;
14+
15+
@BeforeClass
16+
public static void setup(){
17+
test = new _340();
18+
}
19+
20+
@Test
21+
public void test1(){
22+
assertEquals(3, test.lengthOfLongestSubstringKDistinct("eceba", 2));
23+
}
24+
25+
@Test
26+
public void test2(){
27+
assertEquals(0, test.lengthOfLongestSubstringKDistinct("", 0));
28+
}
29+
30+
@Test
31+
public void test3(){
32+
assertEquals(0, test.lengthOfLongestSubstringKDistinct("a", 0));
33+
}
34+
35+
@Test
36+
public void test4(){
37+
assertEquals(1, test.lengthOfLongestSubstringKDistinct("a", 1));
38+
}
39+
40+
@Test
41+
public void test5(){
42+
assertEquals(1, test.lengthOfLongestSubstringKDistinct("a", 2));
43+
}
44+
45+
@Test
46+
public void test6(){
47+
assertEquals(2, test.lengthOfLongestSubstringKDistinct("aa", 1));
48+
}
49+
50+
@Test
51+
public void test7(){
52+
assertEquals(3, test.lengthOfLongestSubstringKDistinct("bacc", 2));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)