10-line O(n) easy Java solution, covering edge cases without extra code
This problem is a basic sliding window problem, so we should have a left and a right index. We will let our right index be the iterator of the string and hold t
This problem is a basic sliding window problem, so we should have a left and a right index. We will let our right index be the iterator of the string and hold the position of the current character we are at. The basic idea is to put the characters you visit in map with their index as the value. We want to find the last occurrence, not the frequency of occurence.  \nOnce we find the character in map, we recalculate max by taking the max value of max and right - left ( length of sliding window in last occurrence, before we found a repeating character), and we recalculate left by taking max of left and the last index our character was found in.\n\n``` \n    public int lengthOfLongestSubstring(String s) {\n        int right, left = 0, max = 0;\n        HashMap<Character, Integer> map = new HashMap<>();\n        for(right = 0; right < s.length(); right++) {\n            if(map.containsKey(s.charAt(right))) {\n               max = Math.max(max, right - left);\n               left = Math.max(left, map.get(s.charAt(right)) + 1); \n            }  \n             map.put(s.charAt(right), right);\n        }\n        return Math.max(max, right - left);\n    }
['Sliding Window', 'Java']