原题链接在这里: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. Linux安装Mycat

    1.官网下载mycat到/home/install目录下 2.解压到/usr/local/mycat目录下 tar -zxvf Mycat-server-1.6-RELEASE-20161028204 ...

  2. Dual Boot WINDOWS 10 and KALI LINUX Easily STEP BY STEP GUIDE截图

    mark. kali安装:https://www.youtube.com/watch?v=KLj2yQPWZDk 删除无用分区:http://www.xitongcheng.com/jiaocheng ...

  3. java常用日期操作方法

    package com.wujiangpo.test.util; import java.text.ParseException; import java.text.SimpleDateFormat; ...

  4. 从引物序列出发查找pcr产物的内容和在基因组上的位置

    1.利用primer_blast工具,找出这对引物序列在基因组上的位置: 结果大概会像这样: 2.这些结果都是根据hg38基因组来定位的,转换成hg19: 利用UCSCde hgLiftover 在线 ...

  5. R中的运算符,条件语句,控制语句

    1.运算符 算术运算符:+,-,*,/ 关系运算符:==,!=,>,>=,<,<= 逻辑运算符:&,|,&&,||,! &和|称为短逻辑符,&a ...

  6. linux下安装casperjs 开发运行环境

    casperjs是一个基于phantomjs的测试框架,使用python进行操作,所以一个完整的casperjs环境需要安装phantomjs和python. 1 phantomjs安装 到官网下载最 ...

  7. iOS_数据存取(一)

    目录: 一.沙盒机制 二.用户偏好设置 三.归档 一.沙盒机制 每个iOS应⽤都有⾃己的应用沙盒(应⽤沙盒就是⽂件系统⽬录),与其他文件系统隔离.应⽤必须待在⾃己的沙盒⾥,其他应用不能访问该应用沙盒的 ...

  8. VS中一个强大的功能,将Json或者XML黏贴为类

    有时候需要传递json,或者是json结构复杂,看的杂乱无章,我们可以将这个json复制下来,然后将它写成类的形式,VS中已经帮我们很好的实现了这个功能,我们只需要选择   编辑===>> ...

  9. Go 语言defer用法

    defer延迟调用: 1.确保调用在函数结束时发生: 2.defer列表为先进后出: 3.通常在Open/Close  Lock/Unlock中使用. defer调用顺序示例: package mai ...

  10. Docker 共享存储解决方案Rex-Ray

    github地址:https://github.com/rexray/rexray 安装: curl -sSL https://rexray.io/install | sh - 生成配置文件: htt ...