Java | TC: O(N) | SC: O(1) | Sliding Window using HashMap & Two Pointers
java\n/**\n * Use HashMap to keep char and its index map. When we find a repeating char\n * update the start point.\n *\n * Time Complexity: O(N)\n *\n * Space 
```java\n/**\n * Use HashMap to keep char and its index map. When we find a repeating char\n * update the start point.\n *\n * Time Complexity: O(N)\n *\n * Space Complexity: O(min(M,N)) = O(1) since there are 26 alphabets.\n *\n * N = Length of input string. M = Size of the character set\n */\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        if (s == null) {\n            throw new IllegalArgumentException("Input string is null");\n        }\n\n        int len = s.length();\n        if (len <= 1) {\n            return len;\n        }\n\n        HashMap<Character, Integer> map = new HashMap<>();\n        int start = 0;\n        int maxLen = 0;\n\n        for (int end = 0; end < len; end++) {\n            char eChar = s.charAt(end);\n            if (map.containsKey(eChar)) {\n                start = Math.max(start, map.get(eChar) + 1);\n            }\n            map.put(eChar, end);\n            maxLen = Math.max(maxLen, end - start + 1);\n        }\n\n        return maxLen;\n    }\n}\n```\n\n---\n\nSolutions to other Sliding Window questions on LeetCode:\n- [76. Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/discuss/1496754/Java-or-TC:-O(S+T)-or-SC:-O(T)-or-Space-optimized-Sliding-Window-using-Two-Pointers)\n- [340. Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/discuss/1496838/Java-or-TC:-O(N)-or-SC:-O(K)-or-One-Pass-Sliding-Window-using-LinkedHashMap)\n- [159. Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/discuss/1496840/Java-or-TC:-O(N)-or-SC:-O(1)-or-One-Pass-Sliding-Window-using-LinkedHashMap)\n- [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1500039/Java-or-TC:-O(S+P)-or-SC:-O(1)-or-Sliding-window-solution)\n- [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/1500877/Java-or-Both-O(N)-and-O(N-logN)-solutions-with-O(1)-space-or-Sliding-Window-and-Binary-Search-solutions)\n- [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/discuss/1500887/Java-or-TC:-O(N)-or-SC:-O(min(N-K))-or-Sliding-Window-using-HashSet)\n- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/discuss/1500895/Java-or-TC:-O(N)-or-SC:-O(min(NK))-or-Sliding-Window-using-Buckets)\n- [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/discuss/1500902/Java-or-TC:-O(S2)-or-SC:-O(1)-or-Constant-space-Sliding-Window-solution)\n- [239. Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/discuss/1506048/Java-or-TC:-O(N)-or-SC:-O(K)-or-Using-Deque-as-Sliding-Window)\n- [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/discuss/1507981/Java-or-TC:-O(N*logK)-or-SC:-(K)-or-Optimized-sliding-window-using-TreeSet)\n- [487. Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/discuss/1508045/Java-or-TC:-O(N)-or-SC:-O(1)-or-Four-solutions-with-Follow-up-handled)\n- [1004. Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/1508044/Java-or-TC:-O(N)-or-SC:-O(1)-or-One-Pass-Optimized-Sliding-Window)\n
['Two Pointers', 'String', 'Sliding Window', 'Java']