Longest Substring with At Most Two Distinct Characters

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.

可与Longest Substring Without Repeating Characters对照看。

这个题很显然使用双指针进行遍历的,begin与end之间的窗口代表符合要求的字符子串。

解法一:

inWindow数组保存窗口中的两个不同字符。(若找不到两个不同的字符,直接返回字符串长度)

map<char, int>保存inWindow中两个字符在窗口中的出现次数。

判断新字符的标准为不等于inWindow数组的任一字符,那么就需要收缩begin,直到inWindow腾出空间给新字符。

class Solution
{
public:
int lengthOfLongestSubstringTwoDistinct(string s)
{
if(s == "")
return ;
else if(s.size() <= )
return s.size(); //slip window [begin, end]
//initial the window with two different chars
int size = s.size();
int begin = ;
int end = ;
while(end < size && s[end] == s[begin])
end ++;
//to here, end == size or s[end] != s[begin]
if(end == size)
return size; //all chars are the same char inWindow[] = {s[begin], s[end]};
map<char, int> m; //char->count map
m[s[begin]] = end-begin; //[begin,end) are all s[begin]
m[s[end]] = ;
int longest = end-begin+;
end ++;
while(end < size)
{
m[s[end]] ++;
if(s[end] == inWindow[] || s[end] == inWindow[])
//in window, extend end
longest = max(longest, end-begin+);
else
{//not in window, shrink begin
//remove a char from window
while(m[inWindow[]] != && m[inWindow[]] != )
{
m[s[begin]] --;
begin ++;
}
//to here, either m[inWindow[0]] == 0 or m[inWindow[1]] == 0
if(m[inWindow[]] == )
inWindow[] = s[end];
else
inWindow[] = s[end];
}
end ++;
}
return longest;
}
};

解法二:

由于A~Z,a~z的ASCII码不超过122,因此开辟128的数组record进行窗口中每个字符的计数。

再设置计数位count,记录当前窗口中有多少个不同的字符。

判断新字符的标准为record值为1(加入新字符之后)。

如果count>2,那么就需要收缩begin,直到s[begin]对应的计数为0,代表少了一类字符,count--

class Solution
{
public:
int lengthOfLongestSubstringTwoDistinct(string s)
{
if(s.size() <= )
return s.size();
int size = s.size();
int record[] = {}; //record the appearance times of each char. Note 'z' is 122, 128 is enough.
int begin = ;
int end = ;
int count = ; //distinct count
int longest = ;
while(end < size)
{
record[s[end]] ++;
if(record[s[end]] == )
//new char
count ++; while(count > )
{//shrink
record[s[begin]] --;
if(record[s[begin]] == )
count --;
//remove one char
begin ++;
}
longest = max(longest, end-begin+);
end ++;
}
return longest;
}
};

我编写的测试用例如下,上述两个解法代码全部通过。

int main()
{ string str1 = ""; //expect: 0 ""
string str2 = "a"; //expect: 1 "a"
string str3 = "aa"; //expect: 2 "aa"
string str4 = "aba"; //expect: 3 "aba"
string str5 = "abcd"; //expect: 2 "ab"
string str6 = "abcdedcba"; //expect: 3 "ded"
string str7 = "abbcdededcba"; //expect: 5 "deded"
string str8 = "eceba"; //expect: 3 "ece"
string str9 = "abaece"; //expect: 3 "aba"
string str10 = "ababcd"; //expect: 4 "abab"
string str11 = "cababcd"; //expect: 4 "abab"
string str12 = "abcdefgabcdefg";//expect: 2 "ab"
string str13 = "ababababababab";//expect: 14 "ababababababab" Solution s;
cout << s.lengthOfLongestSubstringTwoDistinct(str1) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str2) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str3) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str4) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str5) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str6) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str7) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str8) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str9) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str10) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str11) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str12) << endl;
cout << s.lengthOfLongestSubstringTwoDistinct(str13) << endl;
}

【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)的更多相关文章

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

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

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

  5. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  6. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  7. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

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

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

随机推荐

  1. Python科学计算技巧积累四——双y轴图像绘制

    双y轴图像具有单y轴图像没有的对比效果,在MATLAB中有plotyy函数可以实现,Python的实现方式没有MATLAB那样方便,不过实现效果却也不见得差. 以往我常用的绘图命令是import ma ...

  2. Centos curl ssl 替换 NSS 为 OpenSSL

    参考:https://www.latoooo.com/xia_zhe_teng/368.htm 我的系统版本是 Centos 7 64位.为了方便,先安装常用的开发环境. yum groupinsta ...

  3. Centos安装gcc及g++

    Centos支持yum安装,安装软件一般格式为yum install .......,注意安装时要先成为root用户. 按照这个思路,我想安装过程如下: 安装gcc:yum install gcc 安 ...

  4. Eclipse CDT 插件列表

    http://www.bestplugins.com/software/eclipse-c-plugin.html

  5. js的技巧

    字典: if(tac["detail"][sid] || tac["detail"][sid]==0) //判断某项是否存在,0为真 tac["det ...

  6. 推荐一款jQuery ColorPicked 颜色拾取器插件

    先看实现的效果图, 本文底部有完整demo 不想看我墨迹的可以跳过了^_^. 官网地址:http://www.eyecon.ro/colorpicker/#about 代码SVN 地址:https:/ ...

  7. [Canvas]越来越近的女孩

    本作比前作增加了控制功能,观看动态效果请点此下载代码用Chrome或Firfox浏览器观看. 图例: 代码: <!DOCTYPE html> <html lang="utf ...

  8. 8个DBA最常用的监控Oracle数据库的常用shell脚本

    本文介绍了8个常用的监控数据shell脚本.首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron来定时执行DBA脚本.网上也有好多类似的文章,但基本上都不能正常运行,花点时间重 ...

  9. CAD中批量打印

    同事在网上找各种软件来实现CAD图的批量打印,总是问题多多.于是,我想到一个更方便的解决方法,即只要我将一个打印出来,然后就可以用批量处理来实现. 1.在CAD中输入plot命令(或快捷键Ctrl+P ...

  10. WPF 之 创建继承自Window 基类的自定义窗口基类

    开发项目时,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于 Window 自身的,但窗口的外边框及窗口移动.最小化等标题栏操作基本都是一样的.所以通过查看资料,可按如下方法创建继承 ...