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. 设置让php能够以root权限来执行exec() 或者 shell_exec()

    一.查看启动你php的进程的用户是谁. 可以通过在命令行执行:ps -ef | grep php来看.或者在php中执行 echo exec('whoami') 来查看.centos下默认会是nobo ...

  2. 【应用】R--判断类别型属性之间是否有相关性(相互之间是否独立)

    检验某学区所有在售房源中,小区与楼栋类别(低层:多层;小高层:高层)是否相关 导入数据: > house<- read.table("house_data.txt", ...

  3. 微信小程序 this.data与this.setData

    一.摘要 小程序中我们会经常使用到this.data与this.setData.其中this.data是用来获取页面data对象的,而this.setData是用来更新界面的.那么他们之间的区别与联系 ...

  4. 一步一步教你搭建和使用FitNesse

    啄木鸟之家大吕 敏捷测试已成为现在式,尽早和持续的反馈成为各研发团队的必选项.测试同学也需要跟上这个趋势.除了“找bug”.“分析需求”.“功能测试”,还需考虑“交付质量.一次做对.在没有用户界面情况 ...

  5. 【nodejs】理想论坛帖子下载爬虫1.07 使用request模块后稳定多了

    在1.06版本时,访问网页采用的时http.request,但调用次数多以后就问题来了. 寻找别的方案时看到了https://cnodejs.org/topic/53142ef833dbcb076d0 ...

  6. IT行业简报 2014-2-8

    1.微信在“我的银行卡”页面接入嘀嘀打车,三天内微信打车突破10万单,日均订单为70万,其中微信支付订单超过48万单2.三大运营商手机支付用户仅366.3万,与腾讯单月发展手机支付用户500万户相比, ...

  7. 25个Web前端开发工程师必看的国外大牛和酷站

    逛了一周国外大牛们的博客与酷站,真是满满的钦佩.震撼.羡慕.惊喜………… Web设计是一个不断变化的领域,因此掌握最新的发展趋势及技术动向对设计师来说非常重要.无论是学习新技术,还是寻找免费资源与工具 ...

  8. Android Volley 库通过网络获取 JSON 数据

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库通过网络获取 JSON 数据 参考资料 Android 关于网络操作一般都会介绍 HttpC ...

  9. url文件的格式

    [DEFAULT]BASEURL= [InternetShortcut]URL=WorkingDirectory=ShowCommand=IconIndex=IconFile=Modified=Hot ...

  10. Android Studio 之 打包项目生成APK

    本文以使用Android Studio打包第一个apk的角度说明,打包APK分为两步:1生成 jks 密钥:2打包生成APK. 选择“ Build ”→选择“ Generate Signed APK. ...