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.

分析:

  最多包含两个不同字母的子串,至少要遍历一遍,复杂度为O(n),而要实现O(n)复杂度,用移动窗口思想即可。

代码:

int lentwoc(string s) {
if(s.empty())
return ;
char a = s[], b = '\0';
//alast为a字母最后出现的位置,blast为b字母最后出现的位置,lastpos为新两元序列的开端位置,maxl为全局最长序列长度
int alast = , blast = -, lastpos = , maxl = ;
for(int i = ; i < s.length(); i++) {
//开启新两元序列
if(s[i] != a && s[i] != b) {
//记录前一个两元序列长度
maxl = max(i - lastpos, maxl);
if(alast < blast) {
alast = i;
a = s[i];
lastpos = blast;
}
else {
blast = i;
b = s[i];
lastpos = alast;
}
}
else
s[i] == a ? alast = i : blast = i;
}
return maxl;
}

[Locked] Longest Substring with At Most Two Distinct Characters的更多相关文章

  1. [LeetCode] 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] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  3. LeetCode Longest Substring with At Most Two Distinct Characters

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

  4. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

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

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

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

  8. LeetCode 340. Longest Substring with At Most K Distinct Characters

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

  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. 自定义Window 服务

    自定义window 服务 开发到使用的流程: 1.完成对应的代码之后(代码在底下),右键MyService.cs 添加安装程序 2.添加window服务安装程序打开Service1.cs[设计]页面, ...

  2. 开通博客第一天 (先发一些android(java)常见异常信息

    常见异常: java.lang.AbstractMethodError抽象方法错误.当应用试图调用抽象方法时抛出. java.lang.AssertionError断言错.用来指示一个断言失败的情况. ...

  3. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  4. C#.net时间戳转换

    //long ticks = (DateTime.Parse(DateTime.Now.ToString(CultureInfo.InvariantCulture)).ToUniversalTime( ...

  5. Using GUID to generate the unique file name in C#

    GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as ...

  6. sublime text snippet代码片断

    $0 代表补全代码后放的位置   0 的权重是最低的 $1 最高 也等于${1:}  ${1: name}    1输入点的序号(1权重最高) name 自动补全的默认值      <conte ...

  7. c读写文件相关

    1.打开文件: 函数原型: FILE * fopen(const char * path,const char * mode); 返回值: 文件顺利打开后,指向该流的文件指针就会被返回.如果文件打开失 ...

  8. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  9. js 强制转换

    强制转换为布尔类型: <script> var text =Boolean(0) //=>以下转换的类型都为false text = Boolean(0.0) text = Bool ...

  10. PHP-HTML重要知识点笔记

    1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...