原题链接在这里: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. HTML+CSS理解

    HTML+CSS1.对WEB标准以及W3C的理解与认识标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所 ...

  2. 交叉编译Mesa,X11lib,Qt opengl

    记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...

  3. Linux虚拟内存管理(glibc)

    转:https://blog.csdn.net/tengxy_cloud/article/details/53067396 https://www.cnblogs.com/purpleraintear ...

  4. Squid 正向代理配置

    Squid 正向代理配置 1.删除主配置文件重写写入配置 rm -f /etc/squid/squid.conf 2.重新写入配置正向代理 vim /etc/squid/squid.conf # 监听 ...

  5. 超酷Loading进度条

    在线演示 本地下载

  6. HTseq-count

    HTSeq:一个用于处理高通量数据(High-throughout sequencing)的python包.HTSeq包有很多功能类,熟悉python脚本的可以自行编写数据处理脚本.另外,HTSeq也 ...

  7. package.json字段简要解析

    name 必填 应用名称 version 必填 应用版本 description 选填 应用描述,多用于搜索,在npm search 时可以用到 keywords 选填 应用关键字,也多用于搜索 sc ...

  8. awk之腾迅面试题1

    1.题目如下: 3 5 6 72 3 1 04 5 6 92 3 4 42 2 1 04 5 0 9假如把2列和3列的值作为新的第5列,第5列的平均值为avg5,求第5列中大于avg5的行数.  aw ...

  9. 39条常见的linux系统管理面试题

    1.如何看当前Linux系统有几颗物理CPU和每颗CPU的核数? 答:[root@centos6 ~ 10:55 #35]# cat /proc/cpuinfo|grep -c 'physical i ...

  10. Response对象介绍(服务器到客户端)

    1.response的状态码和响应头设置 package com.test; import java.io.IOException; import java.io.PrintWriter; impor ...