import java.io.*;
import java.util.*;
class myCode
{
public int atMost2( String s){
if(s == null ) return 0;
int[] hash = new int[26];
int max = 0;
int start =0;
int count = 0;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
hash[c-'a']++;
if(hash[c-'a'] == 1) count++;
while(count > 2){
char st = s.charAt(start++);
hash[st-'a']--;
if(hash[st-'a'] == 0) count--;
}
max = Math.max(max, i - start + 1);
}
return max;
}
public static void main (String[] args) throws java.lang.Exception
{
myCode test = new myCode();
System.out.println(test.atMost2("eceeeeeeeeeeba"));
}
}
159 Longest Substring with At Most Two Distinct Characters
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Problem More LeetCode DiscussionsGiven a string, find the...
- Given a string, find the length of the longest substring ...
- Given a string, find the length of the longest substring ...
- Given a string, find the length of the longest substring ...
- My code: 这道题目我以前竟然没有做过??自己写了出来,感觉配不上hard的称号。和 longest sub...