My Accepted JAVA Solution


  • 0
    J
    public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() == 0) return 0;
            Map<Character, Integer> map = new HashMap<>();
            int res = 0, curr = 0;
            for(int i = 0; i < s.length(); i++){
                if(!map.containsKey(s.charAt(i)) || i - map.get(s.charAt(i)) > curr){
                    curr++;
                }else{
                    res = Math.max(curr, res);
                    curr = Math.min(curr, i - map.get(s.charAt(i)));
                }
                map.put(s.charAt(i),i);
            }
            res = Math.max(curr, res);
            return res;
        }
    

Log in to reply
 

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