[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
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.
159. Longest Substring with At Most Two Distinct Characters 的拓展,159题是最多有2个不同字符,这里是最多有k个不同字符。
解题方法一样,只是把比较不同字符个数时的2换成k。
Java:
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s == null || s.isEmpty() || k < 1) return 0;
if (s.length() <= k) return s.length(); int result = k;
int pre = 0;
Map<Character, Integer> map = new HashMap<>();
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 p = s.charAt(pre++);
int count = map.get(p) - 1;
if (count == 0) {
map.remove(p);
} else {
map.put(p, count);
}
}
result = Math.max(result, i - pre + 1);
}
return result;
}
}
Java:
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s == null || s.length() == 0 || k <= 0) {
return 0;
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int count = 0;
int start = 0;
int max = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
while (map.size() > k) {
char rm = s.charAt(start);
int tempCount = map.get(rm);
if (tempCount > 1) {
map.put(rm, tempCount - 1);
} else {
map.remove(rm);
}
start++;
count--;
}
}
count++;
max = Math.max(max, count);
}
return max;
}
}
Python:
class Solution(object):
def lengthOfLongestSubstringKDistinct(self, s, k):
longest, start, distinct_count, visited = 0, 0, 0, [0 for _ in xrange(256)]
for i, char in enumerate(s):
if visited[ord(char)] == 0:
distinct_count += 1
visited[ord(char)] += 1 while distinct_count > k:
visited[ord(s[start])] -= 1
if visited[ord(s[start])] == 0:
distinct_count -= 1
start += 1 longest = max(longest, i - start + 1)
return longest
C++:
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
int longest = 0, start = 0, distinct_count = 0;
array<int, 256> visited = {0};
for (int i = 0; i < s.length(); ++i) {
if (visited[s[i]] == 0) {
++distinct_count;
}
++visited[s[i]];
while (distinct_count > k) {
--visited[s[start]];
if (visited[s[start]] == 0) {
--distinct_count;
}
++start;
}
longest = max(longest, i - start + 1);
}
return longest;
}
};
类似题目:
[LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
[LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
All LeetCode Questions List 题目汇总
[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串的更多相关文章
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- [leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- ✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java
Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
随机推荐
- word2vec中的subsampling
http://d0evi1.com/word2vec-subsampling/ 为了度量这种罕见词与高频词间存在不平衡现象,我们使用一个简单的subsampling方法:训练集中的每个词wiwi,以下 ...
- 《基于C/S和B/S混合结构的中职学校教务管理系统设计与实现》论文笔记(十六)
标题:基于C/S和B/S混合结构的中职学校教务管理系统设计与实现 一.基本信息 时间:2008 来源:中 国 海 洋 大 学 关键词:: 教务管理信息系统;C/S和B/S混合结构;UML;USE CA ...
- Java——CaptchaUtil生成二维码乱码
前言 这个问题就是因为Linux上没有字体,你可以有两种方法,一个在生成的时候设置字体,一个就是安装字体. 默认的字体为Courier 乱码情况 步骤 安装字体工具 yum install -y fo ...
- Greenplum 6 新功能 在线扩容工具GPExpand (转载)
Gpexpand是Greenplum数据库的扩容工具,可以为集群增加新节点从而可以存储更多的数据,提供更高的计算能力.Greenplum 5及之前,集群扩容需要停机增加新节点,然后对表数据做重分布.因 ...
- 2019.12.11 java数组练习
class AmHW { public static void main(String[] args) { /* 统计一个公司三个销售小组中每个小组的总 销售额以及整个公司的销售额.如下所示 第一小组 ...
- jedis的连接池
1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...
- scylladb docker-compose 用户密码认证配置
scylladb 对于用户的认证配置还是比较简单的,以下是一个docker-compose 配置的说明 环境准备 docker-compose 文件 version: "3" se ...
- 【dp】P1064 金明的预算方案
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- 【AE软件】视频添加字幕
1.导入视频 2.将视频拖入大屏幕 3.在下面点击右键——新建——文本 4.文字属性设置
- flowable表简要说明
1. Flowable数据库表命名规则 ACT_RE_* ’RE’表示repository(存储).RepositoryService接口操作的表.带此前缀的表包含的是静态信息,如,流程定义,流程的资 ...