###### LeetCode 340. Longest Substring with At Most K Distinct Characters(java)

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.
##### 用伪哈希表和start来标志开头，每次遇到新的哈希表里没有的，就更新start，否则存进哈希表，最后maintain一个最大substring。
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s.length() == 0) return 0;
if (k == 0) return 0;
int result = 1, start = 0, tag = 0, count = 0;

int[] map = new int[256];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map[c] > 0) {
map[c]++;
} else {
//move start
if (count >= k) {
while (tag == 0) {
int num = map[s.charAt(start)];
if (num == 1) {
count--;
tag = 1;
}
map[s.charAt(start)]--;
start++;
}
tag = 0;
}
map[c]++;
count++;
}
result = Math.max(result, i - start + 1);
}
return result;
}

#### LeetCode 340. Longest Substring with At Most K Distinct Characters（最长字串）

2016-04-30 09:10:31

#### [leetcode] 340. Longest Substring with At Most K Distinct Characters 解题报告

2016-04-03 12:28:01

#### Longest Substring with At Most K Distinct Characters

2016-03-29 21:16:24

#### [LeetCode] Longest Substring with At Most Two Distinct Characters及扩展

2015-01-06 04:24:08

#### [leetcode] 159. Longest Substring with At Most Two Distinct Characters 解题报告

2016-03-18 13:38:51

#### leetcode 159: Longest Substring with At Most Two Distinct Characters

2015-01-28 04:05:56

#### Leetcode 395. Longest Substring with At Least K Repeating Characters 至少K重复最长子序列 解题报告

2016-09-06 14:47:47

#### leetcode:3. Longest Substring Without Repeating Characters (java)

2016-06-20 20:36:28

#### 395. Longest Substring with At Least K Repeating Characters

2016-09-05 12:05:39

#### [LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

2015-02-27 12:55:12

## 不良信息举报

LeetCode 340. Longest Substring with At Most K Distinct Characters(java)