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. javascript——获取列表选择行的列值

    表格布局 方法 代码 $("#send").click(function(){ //获取选择的行号 n= $('input:radio[name="radio" ...

  2. Effective JavaScript Item 63 注意异步调用中可能会被忽略的异常

    异常处理是异步编程的一个难点. 在同步的代码中,异常可以非常easy地通过try catch语句来完毕: try { f(); g(); h(); } catch (e) { // handle an ...

  3. 将java项目打包成docker镜像:镜像=副本

    简介:将jar打包成镜像好说,毕竟jar包长的都是一样的,但是我们只是写了一个普通的java项目,我也不方便封装成jar包什么的,但是我们也想打包docker image怎么办呢,我们可以用编译后的j ...

  4. [Canvas]走近的女孩

    动态效果请点此下载文件并使用Chrome或者FireFox浏览器观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> ...

  5. 使用angular5+ionic3+sqlite创建离线app应用

    1.安装sqlite和toast插件 npm install --save @ionic-native/sqlite npm install --save @ionic-native/toast 未完 ...

  6. asp.net使用include包含文件

    么?用asp.net使用include包含文件?……有必要吗?使用“用户控件”不是更好吗? 当然,绝大多数情况下,用户控件都能解决问题.但若要在用户控件中需包含其他公用块,即使用用户控件嵌套,老是出问 ...

  7. Binary Search二分法搜索C++程序

    二分法基本上学计算机的都听过,但是有人不知道的就是其实二分法是减治法的思想. 所谓减治法和分治法有一个主要差别就是减治法是减去一般,就是分治之后只需要解决原问题的一半就可以了得到全局问题的解了.所以速 ...

  8. hadoop mahout 算法和API说明

    org.apache.mahout.cf.taste.hadoop.item.RecommenderJob.main(args) --input 偏好数据路径,文本文件.格式 userid\t ite ...

  9. notepad++一键运行python

    打开notepad++,找到菜单栏的run菜单.   子菜单选中run   弹出的小窗口中,输入cmd /k python "$(FULL_CURRENT_PATH)" & ...

  10. 如何进入docker容器

    http://blog.csdn.net/u010397369/article/details/41045251