Short java AC solution


  • 3
    J
    public int lengthOfLongestSubstring(String s) {
            HashMap<Character, Integer> map = new HashMap<>();
            int len = 0;
            for(int l = 0, r = 0;;r<s.length();r++){
                if(map.containsKey(s.charAt(r))&&map.get(s.charAt(r))>=l)    l = map.get(s.charAt(r))+1;
                map.put(s.charAt(r),r);
                len = Math.max(len, r-l+1);
            }
            return len;
        }
    

    Only a condition to take care of, in which we must only find duplicated char to the right of l.


Log in to reply
 

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