原题链接在这里: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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 340. Longest Substring with At Most K Distinct Characters

    最后更新 二刷 08-Jan-2017 和76.159很像.. 2 pointers.. 通过判断是否每一次是有效读取来增减accumulator,从而判断当前是否符合规定,再来更新maxLength ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. Centos7.2安装bacula及bacula-web

    serverd端安装(centos7) bacula的安装很简单,但是配置文件内容很多,配置不正确服务就启动不了,所以需要用webmin来配置. 1.安装基础软件包: 关闭SElinux(重要)set ...

  2. $Android连续按返回键两次实现退出程序

    思路:重写Activity的onKeyDown方法,判断按键是不是返回键,如果是,则再判断按下的时间和上次按下的时间之间的差值(毫秒数)是不是大于2000,如果不大于,则用finish()方法结束程序 ...

  3. ETL应用:使用shell实现文件级校验的方法

    BI应用中,对接口规范性约束很重要,接口文件提供需要配套提供该文件的校验文件,校验文件格式如下: 序号 信息内容 数据类型及长度 说明 1 接口数据文件名称 CHAR(50) 2 文件的大小(字节数) ...

  4. CMD mysql 备份脚本

    创建.bat文件 echo. echo MySQL数据库备份脚本 echo ***************************** echo. echo 备份日期:%date% echo 备份时间 ...

  5. Vue.js学习笔记 第五篇 事件处理

    监听事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. 高通LCD驱动调试

    本文转载自:http://www.itgo.me/a/x6305658852004979994/lcd%20qcom 来自 :http://blog.csdn.net/dacaozuo/article ...

  7. MVC6 (ASP.NET5) 认证 (Asp.net identity) cookie模式 自定义认证

    1.Startup类的Configure方法中, app.UseIdentity(); 改为 app.UseCookieAuthentication(options => { options.A ...

  8. iostream与iostream.h的区别

    简单来说: .h的是标准C的头文件,没有.h的是标准C++的头文件,两种都是头文件. 造成这两种形式不同的原因,是C++的发展历史决定的,刚才正好有别的人也问这个问题,这里我再回答一下(注意vs200 ...

  9. Maven webapp index.jsp报错

    javax.servlet javax.servlet-api 3.1.0

  10. 多校hdu-5775 Bubble sort(线段树)

    题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...