美文网首页
159 Longest Substring with At M

159 Longest Substring with At M

作者: Fei_JOB | 来源:发表于2017-10-23 11:21 被阅读0次
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 M

      本文链接:https://www.haomeiwen.com/subject/pmwouxtx.html