Simple readable Java by using Hash.


  • 1
    X
    public int lengthOfLongestSubstring(String s) {
        int[] hash = new int[256];
        int max = 0;
        int left = 0, right = 0;
        while(right < s.length() ){
            hash[s.charAt(right)]--;
            while(hash[s.charAt(right)] < -1) // here to check duplicate
                {
                    hash[s.charAt(left)] ++;
                    left ++;
                }
                right++;
                max = Math.max(right - left, max); 
        }
        return max;
    }**bolded text**

Log in to reply
 

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