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. ThinkPHP 5使用 Composer 组件名称可以从https://packagist.org/ 搜索到

    http://www.phpcomposer.com/ 1 这个是国内的composer网站 thinkphp5自带了composer.phar组件,如果没有安装,则需要进行安装 以下命令全部在项目目 ...

  2. Eclipse里面的Maven项目如果下载依赖的jar包的源码

    Window---------Properties---------------Maven--------------勾选Download Artifact Sources和Download Arti ...

  3. spyder快捷键

    ctrl+1:注释/反注释 ctrl+4/5:注释/反注释 tab/ shift+tab:缩进/反缩进 F5:全运行 F9:单行运行 F11:全屏 ctrl+I:显示帮助

  4. delphi WebBrowser的使用方法详解(三)

    WebBrowser 操作记要 WebBrowser1.GoHome;  //到浏览器默认主页 WebBrowser1.Refresh;  //刷新 WebBrowser1.GoBack;  //后退 ...

  5. 使用alter database datafile 'XXX' offline drop 是否能够恢复(非归档模式下)

    今天在群里面听到一位网友在说使用了alter database datafile 'XXX' offline drop命令是否能够恢复数据,在非归档模式下,下面是用一个实验来验证一下 ######## ...

  6. 基于RDBMS的OLAP的解决方案

    BI项目如何开发: 了解OLAP的分析方法: 对数据进行多维建模分析,也就是自己设计自己的数据方体,之后程序自动生成数据方体 数据方体: 1.自动成的表结构,仅仅有你需要的列 2.生成一条SQL语句( ...

  7. jps命令发生异常

    当在集群里输入jps命令时报如下错误: 我就开始检查jdk,感觉应该是centos自动的jdk没卸载干净跟后面安装的jdk冲突 先通过命令 rpm -qa|grep java 查看jdk信息 把这几个 ...

  8. 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)

    项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...

  9. ICML论文|阿尔法狗CTO讲座: AI如何用新型强化学习玩转围棋扑克游戏

    今年8月,Demis Hassabis等人工智能技术先驱们将来到雷锋网“人工智能与机器人创新大会”.在此,我们为大家分享David Silver的论文<不完美信息游戏中的深度强化学习自我对战&g ...

  10. Openstack kvm win7镜像制作

    本文地址http://www.cnblogs.com/tcicy/p/7790956.html 网上找了很多为openstack制作win7镜像的文章,总是不成功 自己写一下,以便大家查看. 我使用c ...