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 ...
随机推荐
- iOS开发项目之三 [ 自定义tabBarCtrl]
01 让tabBar的图片保持原样.图片渲染的处理 ctrl.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithF ...
- MyLinerLayout--推荐
苹果系统的自动布局需要在布局的过程中建立各种各样的依赖,虽然后续使用masonry还算比较便捷. 有一天朋友推荐给我的这个,看了一下demo和这个库的作者的博客,了解到这个库并不是基于苹果的自动布局作 ...
- Css - 文本溢出处理
http://blog.163.com/yinwei_666/blog/static/2036157320101113102733794/ overflow: hidden;-o-text-overf ...
- lucene 3.0.2 基本操作入门
转自:Bannings http://blog.csdn.net/zhangao0086/article/details/ 我们为什么需要Lucene? 任何的的查询功能都类似,都是对文本内容的搜索, ...
- Sql Group by 使用
CREATE TABLE StuCourseScore ( ID int, Name nvarchar(10), Course nvarchar(10), Score int ) INSERT Stu ...
- 投芯片,现在要n+1模式
给坚持理想的屌丝点个赞.投芯片,现在要n+1模式,n个小项目+一个大项目.团队是基础,屌丝创业要以营利为先导,先要短快,同时要有并行的大项目支撑.小项目赚的钱先解决面包问题,同时为大项目锻炼队伍积累经 ...
- Frenetic Python实验(二)
实验3 packet_in_out 目的:模拟一个普通的双端口中继器. This application implements a very simple 2 port repeater where ...
- php安装json模块
centOS上因为看php源码中没有json模块,于是采用pecl自动编译安装:# yum install php-devel# yum install php-pear# yum install g ...
- matches field ID value 17.9.1 The LABEL element
https://www.w3.org/TR/html401/interact/forms.html#edef-LABEL <!ELEMENT LABEL - - (%inline;)* -(LA ...
- Delphi指针及其它(转)
一.指针:指向一个内存地址的变量或参数. 二.定义指针的方式如下: P: Pointer; //定义了可以指向任何类型的指针,Pointer 为无类型指针: Q, R: ^TType; //定义了指向 ...