最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)
[抄题]:
给定一个字符串,找到最多有k个不同字符的最长子字符串。eg:eceba, k = 3, return eceb
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 怎么想到两根指针的:从双层for循环的优化 开始分析
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 没有养成好习惯:退出条件写在添加条件之前。因此先判断if (map.size() == k),再map.put(c,1)
[二刷]:
- 没有养成好习惯:循环过后更新max,循环过后移动指针j,都要在循环之前就写好
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(2n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 字符串中的字母用256[]存比较方便,但是用hashmap存也可以,相同的字母放在一个盒子里,盒子个数达到k时就退出
- 而且hashmap中还有.size()取总长度 .remove()去除key,城会玩 头一次见
[关键模板化代码]:

j用的是while循环,因为第二层不是for,for的特点是必须要从0开始
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
k = 2
[代码风格] :
hashmap判断有没有key不是用contains,而是用containsKey,没怎么注意
public class Solution {
/**
* @param s : A string
* @return : The length of the longest substring
* that contains at most k distinct characters.
*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
//corner case
int maxLen = 0;
HashMap<Character,Integer> map = new HashMap<>();
if (k > s.length()) {
return 0;
}
int i = 0, j = 0;
//i, j go in the same direction
for (i = 0; i < s.length(); i++) {
//put j into hashmap
while (j < s.length()) {
char c = s.charAt(j);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}else {
if (map.size() == k) {
break;
}else {
map.put(c, 1);
}
}
//pointers move first
j++;
}
//renew ans first
maxLen = Math.max(max, j - i);
//remove i if exists
char c = s.charAt(i);
if (map.containsKey(c)) {
int count = map.get(c);
if (count > 1) {
map.put(c, count - 1);
}else {
map.remove(c);
}
}
}
return maxLen;
}
}
最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)的更多相关文章
- [Swift]LeetCode340.最多有K个不同字符的最长子串 $ 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 characte ...
- [Swift]LeetCode159.具有最多两个不同字符的最长子串 $ 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 ...
- [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] 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 ...
- [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 ...
- [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 ...
- [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] 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 ...
随机推荐
- 6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...
- MySQL binlog日志优化
mysql中日志类型有慢查询日志,二进制日志,错误日志,默认情况下,系统只打开错误日志,因为开启日志会产生较大的IO性能消耗. 一般情况下,生成系统中很少打开二进制日志(bin log),bin ...
- MySql 中的 FIND_IN_SET 的使用和相关问题
MySql 中的 FIND_IN_SET 的使用和相关问题 QQ 群里有人讨论如果在 category_ids 中打开 12 的分类,而 category_ids 中的 ID 是以 逗号分开的. 使用 ...
- XSS漏洞攻击原理与解决办法
转自:http://www.frostsky.com/2011/10/xss-hack/ 对于的用户输入中出现XSS漏洞的问题,主要是由于开发人员对XSS了解不足,安全的意识不够造成的.现在让我们来普 ...
- emacs之配置自动安装脚本
emacsConfig下建立install目录,结构大概这样 . ├── auto-complete-etags-setting.el ├── auto-complete-setting.el ├── ...
- php代码编译的实现
1.php是解析型的高级语言,zend内核使用c语言实现,有main函数,php脚本就是输入,内核处理后输出结果,内核将php脚本翻译成c程序可识别的opcode就是php的编译. c语言的编译将c代 ...
- mysql master or master copy
双主复制: 在两台server配置my.cnf [root@localhost mysql]# egrep -v "^$|^#" /etc/my.cnf datadir = /my ...
- [Android] 开发第六天
Android 布局介绍 LinearLayout 线性布局 RelativeLayout 相对布局 TableLayout 表格布局 FrameLayout 帧布局 ConstraintLayout ...
- 十三.jQuery源码解析之$.type()
512行:出现了一个class2type. 在jQuery中全局搜索这个变量. 这段代码的意思是将一串字符串通过空格分割成数组,并且使用each遍历数组来初始化class2type. 最终的结果应该是 ...
- Vue 目录结构分析 数据绑定 绑定属性 循环渲染数据 数据渲染
一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...