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):

 1 public class Solution {
 2     public int lengthOfLongestSubstringTwoDistinct(String s) {
 3         int result = 0;
 4         int first = -1, second = -1;
 5         int win_start = 0;
 6         for(int i = 0; i < s.length(); i ++){
 7             if(first < 0 || s.charAt(first) == s.charAt(i)) first = i;
 8             else if(second < 0 || s.charAt(second) == s.charAt(i)) second = i;
 9             else{
10                 int min = first < second ?  first : second;
11                 win_start = min + 1;
12                 if(first == min) first = i;
13                 else second = i;
14             }
15             result = Math.max(result, i - win_start + 1);
16         }
17         return result;
18     }
19 }

转载于:https://www.cnblogs.com/reynold-lei/p/4247706.html

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 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

    题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...

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

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

  4. [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 two distinct char ...

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

  6. leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法

    题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...

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

  8. 【算法】159题 Longest Substring with at Most Two Distinct Characters 最大的子串

    1.概述 给定一个字符串.查找一个最长子序列,这个子序列中重复的字符最多包含2个. 此问题和 [算法]剑指 Offer 48. 最长不含重复字符的子字符串 问题很相似. 2.方案 2.1 双指针 pu ...

  9. 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串

    链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...

最新文章

  1. 程序员被知乎优化!瞄准bat大厂再次求职!结果让人意外!
  2. java web项目调用rest接口404
  3. 在VM上安装centOS后的网络配置
  4. 文巾解题 136. 只出现一次的数字
  5. Intel INDE(集成原生开发人员体验)的Windows开发指导
  6. 北京西格玛大厦微软社区精英 Visual Studio 2010 技术交流会记录
  7. 应届生想要获取web前端开发岗位?这份技能攻略,面试攻略别错过
  8. C# 判断一个字符串是否为url
  9. 注册时,邮箱自动发送验证
  10. vue - (v-pre、v-cloak、v-once)
  11. c#“集合已修改;可能无法执行枚举操作”
  12. 使用软件测试路由器性能报告,小米路由器网络性能初步测试报告
  13. 搜电影和网盘资源网站
  14. 【自学与引导】 大数据技术 技能课程总结篇
  15. 2019年最新1+x 证书 Web 前端开发中级理论考试(含PDF高清版 )
  16. 智能客服搭建(1) - MRCP Server 搭建
  17. 什么样的DevOps技能是团队需要的?
  18. python猴子分桃_Python 五猴分桃.py问题解答代码
  19. 职称计算机ppt,2017年职称计算机考试(PPT练习题大全)(3)
  20. Simulink 界面模型的矢量图复制

热门文章

  1. 【Linux】一步一步学Linux——lnstat命令(189)
  2. 【Linux】一步一步学Linux——find命令(47)
  3. ARM指令寻址方式之: 内存访问指令寻址
  4. vivo手机解锁_vivo“手术刀”再营业,OriginOS让手机做“减法”
  5. socket连接时间太长受什么原因影响?_真全粮多功能酿酒设备-酿酒发酵前期为什么要通风处理?...
  6. mybatis源码刨析总结
  7. log4j异步mysql_log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor...
  8. 关于iOS沙盒的目录结构和获取
  9. Redis的设计与实现之整数集合和压缩列表
  10. x86汇编-1(第一章—第二章)8086处理器基本情况