[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 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种字符的最长子串的更多相关文章
- [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 ...
- [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 ...
- 【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 ...
- ✡ 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 ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- centos 装iptraf查看网络流量
今天在测试服务器上准备看下网络流量 [root@localhost ~]# iptraf -d eth0-bash: iptraf: command not found 发现运维同事没有给安装 只能自 ...
- git log --author详解,这个是个模糊匹配
git log --author=authorname --author=<pattern>, commits whose author matches any of the given ...
- oracle 年龄计算 岁 月 天
select trunc(months/12) || '岁' || trunc(mod(months, 12)) || '月' || trunc(sysdate - add_months( ...
- mysql 分片
MySQL Fabric(分片) 是一个用于管理 MySQL 服务器群的可扩展框架.该框架实现了两个特性 — 高可用性 (HA ) 以及使用数据分片的横向扩展.这两个特性既可以单独使用,也可以结合使 ...
- servlet里的过滤器filter
过滤器的主要作用 1,任何系统或网站都要判断用户是否登录. 2,网络聊天系统或论坛,功能是过滤非法文字. 3,统一解决编码 怎么创建一个过滤器: 1,生成一个普通的class类,实现Filter接口( ...
- uva579-简单计算题
题意: 求分钟和时钟之间的夹角 解法:俩个夹角互减 AC:10ms #include<iostream> #include<functional> #include<qu ...
- session失效刷新后登录页面嵌入在iframe中的前台解决办法
在前台登录页面中加入JS代码,判断登录页面是否在iframe中,在iframe中就跳转出去 例: //判断是否在iframe中,在里面就跳出去 if (top.location.href != loc ...
- springboot - mybatis - 下划线与驼峰自动转换 mapUnderscoreToCamelCase
以前都是在mybatis.xml中来配置,但是spring boot不想再用xml配置文件.网上搜寻了好久,才找到设置办法:sessionFactoryBean.getObject().getConf ...
- 基于OpenGL编写一个简易的2D渲染框架-06 编写一个粒子系统
在这篇文章中,我将详细说明如何编写一个简易的粒子系统. 粒子系统可以模拟许多效果,下图便是这次的粒子系统的显示效果.为了方便演示,就弄成了一个动图. 图中,同时显示了 7 种不同粒子效果,看上去效果挺 ...
- 基于OpenGL编写一个简易的2D渲染框架-11 重构渲染器-Renderer
假如要渲染一个纯色矩形在窗口上,应该怎么做? 先确定顶点的格式,一个顶点应该包含位置信息 vec3 以及颜色信息 vec4,所以顶点的结构体定义可以这样: struct Vertex { Vec3 p ...