LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
题目:
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
For example, Given s = “eceba”
,
T is "ece" which its length is 3.
题解:
与Longest Substring Without Repeating Characters类似。快慢指针维护substring的方法在Minimum Window Substring里有总结.
runner 扫过 char的frequency加一. 条件count++.
当count > 2后移动walker直到count减到2
Time Complexity: O(n), 窗口只移动一遍. Space: O(1). map size.
AC Java:
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int res = 0;
int [] map = new int[256];
int walker = 0;
int runner = 0;
int count = 0;
while(runner < s.length()){
if(map[s.charAt(runner++)]++ == 0){
count++;
}
while(count > 2){
if(map[s.charAt(walker++)]-- == 1){
count--;
}
}
res = Math.max(res, runner - walker);
}
return res;
}
}
类似Longest Substring with At Most K Distinct Characters, Fruit Into Baskets, Max Consecutive Ones II.
LeetCode Longest Substring with At Most Two Distinct Characters的更多相关文章
- [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 ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- [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 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [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 ...
随机推荐
- c++ stack 的使用
(1) stack::empty bool empty ( ) const; 判断是否为空. return Value : true if the container size is 0, false ...
- 关于isset使用产生Can't use function return value in write context错误
在使用isset检测session的一个取值是否存在时,产生了这个问题 翻译一下:不能在填写的内容中使用函数的返回值.然后我查看了php手册看isset函数的使用:isset()只能用于变量,因为传递 ...
- 菜刀轻松砍杀安全狗 asp一句话中转脚本
看到很多朋友看了我的PHP中转脚本http://phpinfo.me/2014/02/01/309.html ,问我那个脚本只能中转PHP的,但是asp的呢 asp连接的时候安全狗拦截的正是菜刀POS ...
- PHP文件漏桐可以通过对服务器进行设置和配置来达到防范目的
对脚本执行漏洞的防范 黑客利用脚本执行漏洞进行攻击的手段是多种多样的,而且是灵活多变的,对此,必须要采用多种防范方法综合的手段,才能有效防止黑客对脚本执行漏洞进行攻击.这里常用的方法方法有以下四种.一 ...
- [转]3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
本文转自:http://www.cnblogs.com/jirigala/archive/2010/10/07/1845275.html 2010-10-07 21:39 by 通用C#系统架构, 5 ...
- NBUT 1635 Explosion(最小顶点覆盖)
[1635] Explosion 时间限制: 10000 ms 内存限制: 65535 K 问题描述 there is a country which contains n cities connec ...
- checked Exception和unchecked exception
checked Exception: io,sql等exception,程序无法控制的 unchecked exception: 包括Error与RuntimeException及其子类,如:OutO ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5:compile
mvn clean mvn install mvn clean -Dmaven.test.skip=true install 出现上述问题,找不到很多类的一些方法. 解决方法: 1.Window -- ...
- Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)
访问项目地址:http://192.168.254.100/ecshop 某个商品的 URL:http://192.168.254.100/ecshop/goods.php?id=3 现在需要实现把以 ...
- nginx 配置文件分析以及配置负载均衡器
修改Nginx核心配置文件nginx.conf # cat /usr/local/nginx/conf/nginx.conf user www www; worker_processes ; # 工作 ...