【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 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的更多相关文章
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- [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 ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- ✡ 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 ...
随机推荐
- 第二节--Servlet
1.在tomcat的webapp下新建一个web项目test 要有WEB-INF目录,其下有web.xml. 2.WEB-INF下文件是给tomcat使用的 3.用户访问localhost:848 ...
- centos7 网卡命名
CentOS6 及之前以太网网卡进行顺序命名的:多网卡如:eth0,eth1 依次.Centos7 则不同,命名规则默认是基于固件.拓扑.位置信息来分配.一.网卡命名的策略systemd对网络设备的命 ...
- luogu P2680 运输计划
传送门 要最长链的长度最短,一秒想到二分,因为如果对于某个长度满足改掉一边的边权后能使得所有链长度不超过该长度,则所有比他长的长度也满足. 二分最终答案.我们要使得原来长度大于二分的\(mid\)的链 ...
- Maven打包编译找不到com.sun.crypto.provider.SunJCE类
Maven配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>mav ...
- B - 集合选数 (状压DP)
题目链接:https://cn.vjudge.net/contest/281960#problem/B 题目大意:中文题目 具体思路: 我们通过构造矩阵, x , 3x,9x,27x 2x,6x,18 ...
- 11、Logback日志框架介绍和SpringBoot整合实战 2节课
1.新日志框架LogBack介绍 简介:日志介绍和新日志框架Logback讲解 1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等 ...
- Java ArrayList类
ArrayList对象可以用于存储一个对象列表 例子: ArrayList<String> list = new ArrayList<String>() 例子: public ...
- Java打印M图形(二维数组)——(九)
对于平面图形输出集合图形与数字组合的,用二维数组.先在Excel表格中分析一下,找到简单的规律.二维数组的行数为行高,列数为最后一个数大小. 对于减小再增大再减小再增大的,可以用一个boolean标志 ...
- Struts通配符映射
- linux matlab2016 安装
1. 下载Matlab 2016b 下载文件夹中包含三个文件:Matlab 2016b Linux64 Crack.rar,R2016b_glnxa64_dvd1.iso,R2016b_glnxa64 ...