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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 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...