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.

题意:

给定字符串,求至多包含K种字符的最长子串

思路:

[leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串思路大体相同

      j
j
S= “e c e b a” and k = 2, return 3 for "e c e"
i e-0 map.size <=2 move i
i c-1 map.size <=2 move i
i e-2(update) map.size <=2 move i
--------- b-3 map.size >2 get the length then move j
i

代码:

 class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
//corner
if(s.length() < k ) return s.length();
// general
HashMap<Character, Integer> map = new HashMap<>();
int j = 0;
int result = 0;
for(int i = 0; i < s.length(); ){
char c = s.charAt(i);
if(map.size() <= k){
map.put(c, i);
i++;
}
if(map.size() > k){
int leftMost = s.length();
for(int n : map.values()){
leftMost = Math.min(n, leftMost);
} map.remove(s.charAt(leftMost));
j = leftMost + 1;
}
result = Math.max(i - j , result); }
return result; }
}

[leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串的更多相关文章

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

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

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

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

  5. leetcode[159] Longest Substring with At Most Two Distinct Characters

    找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...

  6. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

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

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

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

随机推荐

  1. Linux常见英文报错中文翻译(菜鸟必知)

    Linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permission ...

  2. 服务端tomcat的简单监控

    由于线上对tomcat监控处于失控的状态(只能通过跳转,简单地jstack/jstat进行监控),故需要针对tomcat快速查看其运行状态   Tomcat-manager   在tomcat/web ...

  3. 自己写的jQuery浮动广告插件

    效果图: 文件位置摆放: 插件的js代码: $.extend({ pfAdv:function(options){ var defaults={ count:1, startTop:200, star ...

  4. STL容器能力一览表和各个容器操作函数异常保证

    STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...

  5. unity3d将C#打包成dll方法

    方法一:用vs新建工程-C#库,添加UnityEngine.dll引用,注意.netframwork选3.5,编译C#脚本得到dll: 方法二:使用mono的mcs,具体如下 c#提供了dll打包,但 ...

  6. R语言中的遗传算法详细解析

    前言 人类总是在生活中摸索规律,把规律总结为经验,再把经验传给后人,让后人发现更多的规规律,每一次知识的传递都是一次进化的过程,最终会形成了人类的智慧.自然界规律,让人类适者生存地活了下来,聪明的科学 ...

  7. 如何提取一个转录本的3'UTR区域的序列

    庐州月光 如何提取一个转录本的3'UTR区域的序列 在做microRNA 和 mRNA 相互作用预测的时候,大家都知道microRNA 作用的靶点是位于mRNA 的3'UTR取,所以只需要提取mRNA ...

  8. 进程池----Pool(老的方式)----回调

    之后的进程池使用的是 ProcessPoolExecutor,它的底层使用的就是pool 为什么要有进程池?进程池的概念. 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星 ...

  9. Java 知识点(转)

    1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对应的servlet,并将request.resp ...

  10. form表单提交参数封装

    function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...