Difficulty: Hard

 More:【目录】LeetCode Java实现

Description

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.

Intuition

方法一:假设有一个滑动窗口,窗口左右两边的下标分别记为left和right。遍历字符串,依次确定left和right。具体实现见代码(稍微难理解一点)

方法二:还是引入一个滑动窗口,窗口左右两边的下标分别记为left和right。再引入一个numDistinct变量,用于记录窗口中已经出现的不同字符次数。此外,使用一个int[258]来模拟哈希表,记录每个字符出现的次数count。

  遍历字符串(right右移),当遇到的字符出现次数为0时,numDistinct++; 当numDistinct的次数超过2时,说明left需要往右移,在left往右移的过程中,减少所指字符的count,最终使得numDistinct=2,才暂停left的右移。

  方法二容易理解,且可以用于求解k个不同字符组成的最长子字符。下面代码中方法二就是实现k个不同字符的情况。

Solution

	//Method1: Longest Substring with At Most Two Distinct Characters
public static int lengthOfLongestSubstringTwoDistinct1(String s) {
if(s==null || s.length()<=0)
return 0;
int left=0;
int right=-1;
int maxLen=0;
for(int i=1;i<s.length();i++) {
if(s.charAt(i)==s.charAt(i-1)) continue;
if(right!=-1 && s.charAt(i)!=s.charAt(right)) {
maxLen=Math.max(maxLen, i-left);
left=right+1;
}
right=i-1;
}
return Math.max(s.length()-left,maxLen);
} //Method2: Longest Substring with At Most k Distinct Characters
public static int lengthOfLongestSubstringTwoDistinct2(String s,int k) {
if(s==null || s.length()<=0)
return 0;
int[] count=new int[256];
int numDistinct=0;
int maxLen=0;
int left=0;
for(int right=0;right<s.length();right++) {
if(count[s.charAt(right)]==0) numDistinct++;
count[s.charAt(right)]++;
while(numDistinct>k) {
count[s.charAt(left)]--;
if(count[s.charAt(left)]==0) numDistinct--;
left++;
}
maxLen=Math.max(maxLen, right-left+1);
}
return maxLen;
}

  

Complexity

Time complexity : O(n)

Space complexity :  O(1)

What I've learned

1. 方法一中,i可能会越界,所以代码第16行别忘记了s.length()-left。

2. 方法一里的left,right以及 i指针之间的关系要搞清楚。

3.方法二中引入了numDistinct,非常方便,有时候往往多设置一个变量,就能实现所需要的功能

 More:【目录】LeetCode Java实现

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

  1. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  2. 【leetcode】395. Longest Substring with At Least K Repeating Characters

    题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...

  3. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  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 2 distinct char ...

  5. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  6. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  7. 【LeetCode】3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. 【LeetCode】003. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. ✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

随机推荐

  1. JavaScript之Dom操作【删除当前节点】

    //最新更新:2017-11-25 //现在可以通过更强大而快捷的方式为所有的HTMLElement元素的Dom操作扩展新的方法[注意事项:处理HTMLElemnt元素时,此法对IE-8无效] //原 ...

  2. luogu P2331 [SCOI2005]最大子矩阵

    传送门 \[\huge\mathit{warning}\] \[\small\text{以下说明文字高能,请心脏病,,,,,,人士谨慎观看,请未成年人在家长陪同下观看}\] 皮这一下很开心 其实是代码 ...

  3. luogu P3198 [HNOI2008]遥远的行星

    bzoj 洛谷 这题意是不是不太清楚 真正题意:求\[f_i=\sum_{j=1}^{\lfloor i*A \rfloor} \frac{M_i*M_j}{i-j}\] 似乎只能\(O(n*\lfl ...

  4. 8、判断三角形ABC中是否有点D

    思路: 首先连接AD,BD,CD,SABC为三角形的面积,SABD为三角形ABD的面积,SACD....,SBCD....... 因此,若D在三角形则SABC = SABD + SACD + SBCD ...

  5. C# 对List中的Object进行排序

    首先定义一个List类,这个类里面包含了Name和Total两个属性变量,下面就是针对这两个变量进行排序. public class Player { public string Name { get ...

  6. Django学习手册 - 正则URL路由配置/路由分发

    ############################################### 总结: 一.url路由配置: 方式一:(通过url链接get获取) 方式二:(url路由匹配方式获取-拓 ...

  7. java 两个list 交集 并集 差集 去重复并集

    前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayLi ...

  8. Debian Linux Error “Driver 'pcspkr' is already registered, aborting...”

    问题: Error: Driver ‘pcspkr’ is already registered, aborting… 解决: [root@reistlin.com ~]# echo "bl ...

  9. 【逆向工具】使用x64dbg+spy去除WinRAR5.40(64位)广告弹框

    1 学习目标 WinRAR5.40(64位)的弹框广告去除,由于我的系统为x64版本,所以安装了WinRAR(x64)版本. OD无法调试64位的程序,可以让我熟悉x64dbg进行调试的界面. 其次是 ...

  10. arch linux 安装指南

    (如果不想折腾arch linux,推荐直接使用 manjaro:  https://manjaro.org/ ) 1.安装准备 Arch Linux 能在任何内存空间不小于 512MB 的 x86_ ...