LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/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.
题解:
类似Longest Substring with At Most Two Distinct Characters.
2变成k即可.
Time Complexity: O(n), n = s.length(). Space: O(1), map size.
AC Java:
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int res = 0;
int [] map = new int[256];
int walker = 0;
int runner = 0;
int count = 0;
while(runner < s.length()){
if(map[s.charAt(runner++)]++ == 0){
count++;
}
while(count > k){
if(map[s.charAt(walker++)]-- == 1){
count--;
}
}
res = Math.max(res, runner - walker);
}
return res;
}
}
跟上Longest Repeating Character Replacement.
LeetCode 340. Longest Substring with At Most K Distinct Characters的更多相关文章
- [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 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 340. Longest Substring with At Most K Distinct Characters
最后更新 二刷 08-Jan-2017 和76.159很像.. 2 pointers.. 通过判断是否每一次是有效读取来增减accumulator,从而判断当前是否符合规定,再来更新maxLength ...
- [LeetCode] 395. Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [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 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 K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- 【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: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
随机推荐
- Scalability, Availability & Stability Patterns
https://blog.csdn.net/ajian005/article/details/6191814 一 自我有要求的读者应该提出问题:(研习:掌握层次:)能力级别:不会(了解)——领会( ...
- Cgroups控制cpu,内存,io示例【转】
本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...
- MVC6 (ASP.NET5) 认证 (Asp.net identity) cookie模式 自定义认证
1.Startup类的Configure方法中, app.UseIdentity(); 改为 app.UseCookieAuthentication(options => { options.A ...
- MongoDB快速入门(四)- 插入文档
插入文档 将数据插入到MongoDB集合,需要使用MongoDB 的 insert() 方法. 语法 insert()命令的基本语法如下: >db.COLLECTION_NAME.insert( ...
- juniper常用命令(二)
Juniper防火墙基本命令 常用查看命令 Get int查看接口配置信息 Get int ethx/x查看指定接口配置信息 Get mip查看映射ip关系 Get route查看路由表 Get po ...
- Eclipse常用快捷键(转帖)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- UVA 11186 Circum Triangle (枚举三角形优化)(转)
题意:圆上有n个点,求出这n个点组成的所有三角形的面积之和 题解: 当我们要求出S(i,j,k)时,我们需要假设k在j的左侧,k在i与j之间,k在i的右侧. 如果k在 j的左侧 那么 S(i,j,k ...
- Redis简介 & 与Memcache的区别
redis 是一个基于内存的高性能key-value数据库. Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操 ...
- Excel 导入到处问题处理!
1.未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 因为没有安装64位的Jet40驱动.可以到 http://www.microsoft.com/downl ...
- php 实现微信模拟登陆、获取用户列表及群发消息功能示例
本文实例讲述了php实现微信模拟登陆.获取用户列表及群发消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...