Java Short O(n) solution


  • 0
    H
    public int lengthOfLongestSubstring(String s) {
        if(s=="" ) return 0;
        int[] freq=new int[256];
        int max=0;
        int i=0,j=0;
        int count=0;
        while(j<s.length()){
            if(freq[s.charAt(j++)]++ > 0) count++;
            while(count>0 ) 
                if(freq[s.charAt(i++)]-->1)
                    count--;
            max=Math.max(max,j-i);
        }
        return max;
        
    }
    

    }


  • 0
    J

    sorry, I cannot understand what line8 means, could you explain?
    Thank you so much!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.