原题链接在这里: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 CharactersFruit Into BasketsMax Consecutive Ones II.

LeetCode Longest Substring with At Most Two Distinct Characters的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  4. 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 ...

  5. 【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 ...

  6. [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 ...

  7. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Java线程之CompletionService

    转自:http://blog.csdn.net/andycpp/article/details/8902699 当使用ExecutorService启动了多个Callable后,每个Callable会 ...

  2. Response.Write 打开新窗口

    Response.Write 打开新窗口 string url=" XXX.aspx?aaa="+bbb"; Response.Write(" <scri ...

  3. lucene索引日期和数字

    1.用途. 索引数字的场景主要有两种:一是把它们当作字符串一样处理,比如“要是搁以前,术士能暴击10000多,有木有!”中的"10000",它和其它的词没什么区别,你可以把它仅仅想 ...

  4. Git Shell使用笔记

    1,首次打开Git shell错误(以前打开过gethub客户端) 警告: git command could not be found. Please create an alias or add ...

  5. CSS3:动画大全

    和过渡的区别 页面不用明显js调用: 过渡:必须有:hover visited 等伪类调用.(本质还是事件驱动) 动画:页面加载上就可以. 页面有js调用: 7个参数,*为可选 animation-n ...

  6. HRBUST 1326 循环找父节点神术

    题意 给出一个图 给出a点到每个点的路径 最后经过的除这个点本身以外的点 现在把a点改为b点 让求出按上面那种方式 把除b之外的点对应的点列出 ...算了我描述题意得能力好差...这个锅还是给出题的吧 ...

  7. Apache Spark技术实战之1 -- KafkaWordCount

    欢迎转载,转载请注明出处,徽沪一郎. 概要 Spark应用开发实践性非常强,很多时候可能都会将时间花费在环境的搭建和运行上,如果有一个比较好的指导将会大大的缩短应用开发流程.Spark Streami ...

  8. DS实验题 Floyd最短路径 & Prim最小生成树

    题目: 提示: Floyd最短路径算法实现(未测试): // // main.cpp // Alg_Floyd_playgame // // Created by wasdns on 16/11/19 ...

  9. HTTP 笔记与总结(1 )Telnet 分别发送 HTTP GET 和 HTTP POST 请求

    简化的说: WebService = HTTP 协议 + XML Rest = HTTP 协议 + Json 各种 API = HTTP 协议 + XML / Json 来实现 HTTP 请求信息格式 ...

  10. thinkphp 设计思想

    thinkphp 运行机制: thinkphp首先设置一些常量.然后运行Think对象(系统环境),装载think的运行环境,包括:自动加载.异常处理.功能核心等.环境准备好后,运行App对象(应用程 ...