【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 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)的更多相关文章
- [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 ...
- [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 ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [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】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- ✡ 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 ...
- [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 ...
随机推荐
- css3中webkit内核的滚动栏样式
项目其中用到的滚动栏样式,在别人的基础上调成适合自己的样式.(IE能够调试滚动栏样式,firefox眼下不能调试) ::-webkit-scrollbar { width: 14px; }/* Tra ...
- 【大数据】大数据处理-Lambda架构-Kappa架构
大数据处理-Lambda架构-Kappa架构 elasticsearch-head Elasticsearch-sql client NLPchina/elasticsearch-sql: Use S ...
- 转:CRF++
CRF++使用小结 http://www.cnblogs.com/pangxiaodong/archive/2011/11/21/2256264.html 1. 简述 最近要应用CRF模型,进行序列 ...
- c# event Action 判断事件列表中是否存在这个委托
using UnityEngine; using System.Collections; using System; public class eventTest : MonoBehaviour { ...
- form表单自动回车提交
对于使用了submit按钮的form表单,浏览器会直接建立回车与submit按钮之间的关联
- spring结合mockito
转载:https://blog.csdn.net/apexlj/article/details/50298029 mockito可模拟依赖对象的行为,从而简化跨类的测试. maven获取mockito ...
- [Canvas]更多的球
欲观看动态效果请点此下载代码并用Chrome或者Firefox打开. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> ...
- iOS的settings bundle中开关button(Toggle Switch)取不到值的问题
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 在Xcode7.2中设置App的settings bundle ...
- iOS 开发者证书总结 in-house
iOS 证书分两种类型. 第一种为$99美元的,这种账号有个人和公司的区别,公司账号能创建多个子账号,但个人的不能.这种账号可以用来上传app store 第二种为¥299美元的,这种账号只能用于企业 ...
- Nginx+Lua+Redis构建高并发应用
一. 源文来自:http://www.ttlsa.com/nginx/nginx-lua-redis/ 二. 预览如下: