[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 ...
随机推荐
- 动态生成16位不重复随机数、随机创建2位ID
/** 1. * 动态生成16位不重复随机数 * * @return */ public synchronized static String generate16() { StringBuffer ...
- requireJS的基本使用
requireJS的基本使用 一.总结 一句话总结: requireJS是js端模块化开发,主要是实现js的异步加载,和管理模块之间的依赖关系,便于代码的编写和维护 1.页面加载的js文件过多的缺点是 ...
- iptables 规则学习
iptables 一共有 3 张表:mangle,nat,filter mangle 表主要处理 ttl,tos,mark 等信息(进) filter 顾名思义就是过滤器,用作防火墙(出) nat 主 ...
- MySQL 日期时间相关函数
第一部分:时间差函数 timestampdiff.datediff.timediff 一.时间差函数:timestampdiff 语法:timestampdiff(interval, datetim ...
- postgres常用运维sql
1.查看数据库大小 select pg_database_size('log_analysis'); postgres=# select pg_database_size('postExpress') ...
- Ajax的个人总结
Ajax Ajax是Asynchronous Javascript And XML(异步JavaScript和XML)的缩写. Ajax技术描述了使用脚本操纵HTTP和Web服务器进行数据交换,在页面 ...
- 2019.12.09 java循环(do……while)
class Demo05{ public static void main(String[] args) { int sum=0; int i=1; do{ sum+=i; i++; }while(i ...
- The database returned no natively generated identity value错误解决方案
原因:hibernate项目中在学生表的配置文件中: <id name="studentno" column="studentno"> <ge ...
- JMX脚本在某些机器上报错,有的运行超时
运行超时的 是因为在server端运行命令执行脚本,是server给agent下达的指定,但是server端到agent的10050端口没开,所以或一致堵死在那,知道执行超时, 解决:开通server ...
- gitbook+git+typora 的使用过程
Typora 下载地址:https://typora.io/ gitbook 第一步:安装 npm install -g gitbook-cli 第二步:使用 对要操作的文件夹执行命令 gitbook ...