Given a string S, find the length of the longest substring T that contains at most two distinct characters.
For example,
Given S = "eceba",
T is "ece" which its length is 3.

给一个字符串,找出最多有两个不同字符的最长子串。还是滑动窗口法,定义一个HashMap或者Array来存储出现过的字符个数,左右指针初始位子为0,右指针向右扫,扫到新字符不同字符数加1,存入HashMap,扫到出现过的字符,HashMap对应字符数量加1。如果不同字符数大于2了,就把左指针位置的字符在HashMap中的数量减1,注意不一定是这一个,如果此时这个字符的数量大于0,说明还有这个字符存在,左指针加1继续右移,扫到的字符以后不会在窗口里了,要在HashMap中减1,如果这个字符减1后为0了,说明这个字符在窗口中没有了,此时不同字符数就可减1,左指针在右移1位指向下一个字符,统计此时窗口长度与最大长度比较,保留最大值。重复上面步骤,直到右指针扫完所有字符。最后返回最大长度。

Java: T: O(n), S: O(1)

public int lengthOfLongestSubstringTwoDistinct(String s) {int max=0;HashMap<Character,Integer> map = new HashMap<Character, Integer>();int start=0;for(int i=0; i<s.length(); i++){char c = s.charAt(i);if(map.containsKey(c)){map.put(c, map.get(c)+1);}else{map.put(c,1);}if(map.size()>2){max = Math.max(max, i-start);while(map.size()>2){char t = s.charAt(start);int count = map.get(t);if(count>1){map.put(t, count-1);}else{map.remove(t);}start++;}}}max = Math.max(max, s.length()-start);return max;
}

Java:

public int lengthOfLongestSubstringTwoDistinct(String s) {Map<Character, Integer> map = new HashMap<>();int start = 0, end = 0;int counter = 0, len = 0;while(end < s.length()){char cur = s.charAt(end);if(!map.containsKey(cur)) map.put(cur, 0);map.put(cur, map.get(cur) + 1);if(map.get(cur) == 1) counter++;//new distinct charwhile(counter > 2){//char c2 = s.charAt(start);map.put(c2, map.get(c2) - 1);if(map.get(c2) == 0) counter--;start++;}len = Math.max(end - start + 1, len);end++;}return len;}

  

Python:

class Solution:def lengthOfLongestSubstringTwoDistinct(self, s):longest, start, distinct_count, visited = 0, 0, 0, [0 for _ in xrange(256)]for i, char in enumerate(s):if visited[ord(char)] == 0:distinct_count += 1visited[ord(char)] += 1while distinct_count > 2:visited[ord(s[start])] -= 1if visited[ord(s[start])] == 0:distinct_count -= 1start += 1longest = max(longest, i - start + 1)return longest

C++:

class Solution {
public:int lengthOfLongestSubstringTwoDistinct(string s) {int res = 0, left = 0;unordered_map<char, int> m;for (int i = 0; i < s.size(); ++i) {++m[s[i]];while (m.size() > 2) {if (--m[s[left]] == 0) m.erase(s[left]);++left;}res = max(res, i - left + 1);}return res;}
};

    

类似题目:

[LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

All LeetCode Questions List 题目汇总

转载于:https://www.cnblogs.com/lightwindy/p/8526298.html

[LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...相关推荐

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

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

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

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

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

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

  6. LeetCode 159. 至多包含两个不同字符的最长子串(滑动窗口)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个字符串 s ,找出 至多 包含两个不同字符的最长子串 t ,并返回该子串的长度. 示例 1: 输入: "eceba" 输出: ...

  7. LeetCode 159. 至多包含两个不同字符的最长子串 (滑动窗口哈希表)

    159. 至多包含两个不同字符的最长子串 class Solution {public int lengthOfLongestSubstringTwoDistinct(String s) {// 记录 ...

  8. 力扣(LeetCode)159. 至多包含两个不同字符的最长子串(2022.06.08)

    给定一个字符串 s ,找出 至多 包含两个不同字符的最长子串 t ,并返回该子串的长度. 示例 1: 输入: "eceba" 输出: 3 解释: t 是 "ece&quo ...

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

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

最新文章

  1. 实例:使用puppeteer headless方式抓取JS网页
  2. 【开源项目】一个GitHub项目,囊括人脸图像要读的重要论文
  3. 一秒创建高级查询服务
  4. docker 部署java_使用Java EE 7,WildFly和Docker进行持续部署–(第1部分)
  5. oracle 之 基础操作
  6. 【JAVA 第三章 流程控制语句】课后习题 判断用户输入的数是否为质数
  7. ubuntu右键添加打开终端的快捷菜单
  8. 成为百万富翁的25种方法
  9. ubuntu安装python_与你分享Ubuntu修改源和安装pycharm
  10. QT每日一练day5:QLabel和按钮窗口打印功能
  11. 高斯双边模糊_OpenCV 学习:9 双边滤波bilateralFilter
  12. Vue 作者尤雨溪:以匠人的态度不断打磨完善 Vue
  13. android如何设置透明字体颜色,Android设置字体透明度
  14. 研究生英语写译教程(提高级/第三版)_翻译篇 笔记及答案
  15. 第四届“云鼎奖”网络投票火热进行中——入围名单一览
  16. Hadoop之——伪分布安装
  17. 操作系统怎样控制硬件
  18. 水果店圈子:水果店坏水果应该怎么处理,水果店卖剩下的水果如何处理
  19. 编解码base64、对称加密aes和非对称加密rsa
  20. ROP Emporium ret2csu

热门文章

  1. UVALive 6909 Kevin's Problem 数学排列组合
  2. 图 | 为什么存在关于图的研究
  3. ubuntu12.04 qtcreate支持中文输入
  4. Spring配置JPA的xml路径的问题
  5. ★LeetCode(292)——Nim 游戏(JavaScript)
  6. 关于数据属性特性configurable设置为false后的限制
  7. postgresql 先创建唯一主键 再分区_PostgreSQL 务实应用(三/5)分表复制
  8. netsh winsock reset什么意思_IOS14.2rc是什么意思?ios14.2rc怎么样?[多图]-手机资讯...
  9. 听说养老保险可以在手机APP上年审,有知道怎么年审的吗?
  10. 给汽车轮胎内充满水而不是气,行不行?