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.

用p1 & p2 两个pointer分别纪录当前window里两个character最后一次发生时的index,用start纪录window开始时的index。
从index 0开始遍历String:

如果当前的character在window内,update相应的pointer。

如果不在,比较两个character的pointer,去掉出现较早的那个character, 更新start=min(p1,p2)+1

时间复杂度是O(n), 空间复杂度是O(1):

 public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int result = 0;
int first = -1, second = -1;
int win_start = 0;
for(int i = 0; i < s.length(); i ++){
if(first < 0 || s.charAt(first) == s.charAt(i)) first = i;
else if(second < 0 || s.charAt(second) == s.charAt(i)) second = i;
else{
int min = first < second ? first : second;
win_start = min + 1;
if(first == min) first = i;
else second = i;
}
result = Math.max(result, i - win_start + 1);
}
return result;
}
}

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

  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 Two Distinct Characters

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

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

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

  5. [Locked] Longest Substring with At Most Two Distinct Characters

    Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...

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

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

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

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

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

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

随机推荐

  1. 2_C语言中的数据类型 (七)printf与scanf

    1          字符串格式化输出和输入 1.1       字符串在计算机内部的存储方式 字符串是内存中一段连续的char空间,以’\0’结尾 “”是C语言表达字符串的方式 1.2       ...

  2. cjoj P1435 - 【模板题 USACO】AC自动机 && 洛谷 P3796 【模板】AC自动机(加强版)

    又打了一遍AC自动稽. 海星. 好像是第一次打trie图,很久以前就听闻这个思想了.OrzYYB~ // It is made by XZZ #include<cstdio> #inclu ...

  3. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  4. Frida----基本代码

    代码来自官网:https://www.frida.re/docs/examples/android/ import frida, sys def on_message(message, data): ...

  5. windows 平台安装 ffmpeg

    一.从https://ffmpeg.zeranoe.com/builds/中下载ffmpeg的static版本: 二.将下载下来的“ffmpeg-4.0.2-win64-static.zip”解压到任 ...

  6. WebSocket抓包分析

    转载自:https://www.cnblogs.com/songwenjie/p/8575579.html Chrome控制台 (1)F12进入控制台,点击Network,选中ws栏,注意选中Filt ...

  7. [linux] /sbin/nologin的用户运行程序

    创建一个/sbin/nologin用户 [root@host101 ~]# useradd -s /sbin/nologin redis [root@host101 ~]# grep redis /e ...

  8. Scrum立会报告+燃尽图(十月十一日总第二次):需求分析

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2191 Scrum立会master:张俊余 一.小组介绍 组长:付佳 组员 ...

  9. 【Alpha】阶段第六次Scrum Meeting

    [Alpha]阶段第六次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 增加教师接口 增加上课信息接口 赵智源 构建后端测试点测试框架 构建后测试点测试框架 肖 ...

  10. android学习-2 (AVD 创建)

    在Android studio的tools下选择AVD manager 按照指示选择相应的硬件和系统映像. 在模拟器中运行应用 选择RUN APP 选择RUN时,并不只运行应用,还会处理运行应用所需要 ...