Longest Substring with At Most Two Distinct
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.
用p1 & p2 两个pointer分别纪录当前window里两个character最后一次发生时的index,用start纪录window开始时的index。
从index 0开始遍历String:
如果当前的character在window内,update相应的pointer。
如果不在,比较两个character的pointer,去掉出现较早的那个character, 更新start=min(p1,p2)+1
时间复杂度是O(n), 空间复杂度是O(1):
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int result = 0;
int first = -1, second = -1;
int win_start = 0;
for(int i = 0; i < s.length(); i ++){
if(first < 0 || s.charAt(first) == s.charAt(i)) first = i;
else if(second < 0 || s.charAt(second) == s.charAt(i)) second = i;
else{
int min = first < second ? first : second;
win_start = min + 1;
if(first == min) first = i;
else second = i;
}
result = Math.max(result, i - win_start + 1);
}
return result;
}
}
Longest Substring with At Most Two Distinct的更多相关文章
- [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 ...
- [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 ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- [Locked] Longest Substring with At Most Two Distinct Characters
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- 【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 ...
- 【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 ...
- [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 ...
随机推荐
- idea中 和outline相似的功能
ctrl+ F12 alt+7 ctrl+h 这个用来看继承关系很好
- idea ssm项目出现日志中文乱码,封装的json中的msg字段中文乱码(但是json封装的bean中的字段不乱码)等其他各种项目下的中文乱码解决方案
开头划重点!(敲黑板):rebuild和mvn package的循环往复好几次的操作是解决这个问题的最主要的方法! 经过多次试验,发现这样做就可以正常显示中文了 我说为什么有时候乱码,有时候中文正常, ...
- java多线程的简单demo
模拟场景:顾客买车从车库中取车,厂家生产车,车存储在车库中.买家.厂家对同一个车库中的车操作 一.不加同步机制的代码如下: package com.joysuch.testng.thread; imp ...
- linux bash Shell脚本经典 Fork炸弹演示及命令详解
Jaromil 在 2002 年设计了最为精简的一个Linux Fork炸弹,整个代码只有13个字符,在 shell 中运行后几秒后系统就会宕机: :(){:|:&};: 这样看起来不是很好理 ...
- ROC曲线与AUC
一.ROC曲线 1.简介 ROC曲线全称是"受试者工作特征曲线 "(Receiver Operating Characteristic curve),又称为感受性曲线(Sensit ...
- (转载)面试题收集——Java基础部分(一)
转自:http://www.cnblogs.com/xdp-gacl/p/3641769.html 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可 ...
- 报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverT
报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or repr ...
- Unity3D — — Inspector面板编辑
转载官方文档,暂未深入研究 PropertyDrawer
- 苏州地区--校招IT公司
完整经历了苏州的秋招和春招,在本校和苏州大学跑了许多次的宣讲会,自认为对苏州IT企业的校招有一个充分的认知.原本打算在苏州找一份Java开发的工作,可是发现自己简历连那些公司的简历关都过不去(对双非学 ...
- thrift0.5入门操作
在探索未知的程序之前,我们往往会使用“Hello World”这个经典的输出作为测试,为了遵循这个惯例,作为thrift菜鸟都不算的一员,决定跑一下“Hello world”正式进入菜鸟的行列. th ...