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. Web 数据源

    问题描述: ClassCastException:类型转换异常 问题代码: private static LinkedList<Connection> pool = (LinkedList ...

  2. 根据inode编号来删除文件或目录

    在Linux系统上,有时候会出现文件名为特殊字符的文件或目录,当我们使用rm来删除这样的文件或目录时,就会出错导致删不掉.但是我们可以依据inode号来删除这样的文件,方法如下: (1)执行ls -i ...

  3. 20180129周一之学习PYTHON笔记【PYTHON2写个自动点击学习功能】

    pyautogui.click(pyautogui.center(pyautogui.locateOnScreen('sy.png'))) #点击该截图一次 --------------------- ...

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

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

  5. selenium+python自动化94-行为事件(ActionChains)源码详解

    ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互. 这对于做更复杂的动作非常有用,比如悬停 ...

  6. c++官方文档-模版类

    #include <iostream> using namespace std; template<class T> class MyPair { private: T t[] ...

  7. 多数据源springboot-jta-atomikos

    参考:  https://github.com/classloader/springboot-jta-atomikos-demo 參考:二 :建议参考  https://blog.csdn.net/a ...

  8. 关于jquery的cookie的顺序,应首先是jQuery的引用,然后是cookie引用,否则系统无反应

    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript" ></scri ...

  9. linux 下常用部分命令

    关机 (系统的关机.重启以及登出 ) shutdown -h now 关闭系统() init 关闭系统() shutdown -h hours:minutes & 按预定时间关闭系统 shut ...

  10. Django---form 详解

    Form表单的功能 准备数据.重构数据,以便下一步提交. 为数据创建HTML 表单 接收并处理客户端提交的表单和数据 普通字段详解: class BooleanField(**kwargs): 默认的 ...