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.

给一个字符串,求这个字符串中,由两个字母组成的,连续的最长子串的长度。

虽然是hard,但是感觉并没有什么难度。

用ch1和preCh记录当前两个字母,preCh记录的是上一个字母(s.charAt(i-1)),same记录的是preCh这个字母重复出现的次数,这样出现第三个字母的时候,就可以直接得出从0到i由后两个字母组成的长度为same+1,并没有使用其他的数据结构。

时间O(n),空间O(1).

public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int len = s.length();
if (len < 3){
return len;
}
char ch1 = s.charAt(0);
int same = 0;
while (same < len && s.charAt(same) == ch1){
same++;
}
if (same == len){
return len;
}
char preCh = s.charAt(same);
int result = same + 1;
int ans = same + 1;
int i = same + 1;
same = 1;
for (; i < len; i++){
if (s.charAt(i) == preCh){
result++;
same++;
} else if (s.charAt(i) == ch1){
result++;
same = 1;
ch1 = preCh;
preCh = s.charAt(i);
} else {
ch1 = preCh;
preCh = s.charAt(i);
ans = Math.max(ans, result);
result = same + 1;
same = 1;
}
}
ans = Math.max(ans, result);
return ans;
}
}

2、还可以利用hashMap来做:(参考discuss)

  Map数目小于3的时候将字母和他的位置加入Map中。

  如果是大于等于3,那么找出距离位置hi最远的一个字母(leftMost),删掉,从leftMost的下一个字母开始到当前位置hi就是当前两个字母的长度。

public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
if(s.length() < 1) return 0;
HashMap<Character,Integer> index = new HashMap<Character,Integer>();
int lo = 0;
int hi = 0;
int maxLength = 0;
while(hi < s.length()) {
if(index.size() <= 2) {
char c = s.charAt(hi);
index.put(c, hi);
hi++;
}
if(index.size() > 2) {
int leftMost = s.length();
for(int i : index.values()) {
leftMost = Math.min(leftMost,i);
}
char c = s.charAt(leftMost);
index.remove(c);
lo = leftMost+1;
}
maxLength = Math.max(maxLength, hi-lo);
}
return maxLength;
}
}

3、其实不算算法,就是把s先转换成char[]。这样就会达到最快。

public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int len = s.length();
if (len < 3){
return len;
}
char[] words = s.toCharArray();
char ch1 = words[0];
int i = 1;
while (i < len && words[i] == ch1){
i++;
}
if (i == len){
return len;
}
int same = 1;
char preCh = words[i];
int ans = i + 1;
int result = ans;
i++;
while (i < len){
if (words[i] == preCh){
result++;
same++;
} else if (words[i] == ch1){
result++;
same = 1;
ch1 = preCh;
preCh = words[i];
} else {
ch1 = preCh;
preCh = words[i];
ans = Math.max(ans, result);
result = same + 1;
same = 1;
}
i++;
}
ans = Math.max(ans, result);
return ans;
}
}

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

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

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

  3. leetcode[159] Longest Substring with At Most Two Distinct Characters

    找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...

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

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

  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 340. Longest Substring with At Most K Distinct Characters

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

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

  9. [LC] 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. KO Demo

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. Python编程练习题

    1 求可用被17整除的所有三位数 for num in range(99,1000): if num % 17 == 0: print num ps:下面的写法和上面的写法性能的差距,上面好吧? fo ...

  3. 将php网站移到CentOS 6.7上[二]:将网站部署到服务器上

    首先,确保lamp环境已安装好.准备好项目源代码,数据库备份文件等.由于没有安装好VNC,因此只能用ssh部署了. 将项目源代码压缩,Linux默认是支持SFTP的,用SFTP将源代码压缩包上传到 / ...

  4. python 中的input

    渣渣之路. 一. 在python编程初学者指南中的第六章.使用参数和返回值的例子中: # -*- coding: utf-8 -*- def display(message): print messa ...

  5. HttpHelper类登录淘宝联盟并下载淘宝客订单xls

    本次开发环境与工具如下:IE9.0浏览器 + IE抓包插件HttpWatch +WIN7 64位系统 + VS2005 IDE + .NET 2.0框架本想上传HttpWatch抓包插件,但由于文件超 ...

  6. [讨论] win7封装时如何直接开通局域网共享

    ekincheng 发表于 2016-10-31 20:17:54 https://www.itsk.com/thread-371838-1-5.html Win7封装时不能像XP那样直接开启局域网共 ...

  7. display:inline 和display:inline-block和display:block的区别

    之前讲过块级元素使用display:block 行内元素使用display:inline 那么今天我们就来区分一下display:inline,display:inline-block和display ...

  8. c/c++ 软件集成 安装和可卸载软件

    作为一个工程师应具备的一些能力: 1. 首先具备这款软件:  >inno  Setup      免费版还开源,良心货,妥妥的. 2. 这款软件上手也比较款,可自行参考使用文档 3.编译成功,生 ...

  9. [Weekly] 2014.03.01-2014.03.08

    这周写过好多东西,虽然还没有完全弄明白线段树,但是progress还是有的! 不过有时候真的很想哭,因为自己的梦想连别人看看韩剧.无所事事还要分量轻,实在不明白政治课的Teamwork意义何在,花两分 ...

  10. 基于百度定位及天气获取的DEMO +fragment+sharedpreference

    此工程较BaiduLocationXML相比:1.植入fragment,结合微信UI2.在原本主界面的button  textview  textview 移植到Fragment13.增加网络判断,网 ...