Java | Sliding Window template
Algo:\n1. Use a hashMap to keep track of the latest index of each letter\n2. Keeping the left pointer at rest, move the right pointer by 1 letter at a time.\n3.
Algo:\n1. Use a hashMap to keep track of the latest index of each letter\n2. Keeping the left pointer at rest, move the right pointer by 1 letter at a time.\n3. When a repeating character is encountered, update the maxLength and move the left pointer to max{left pointer, old last occurence of this character as available in the map}. We do a max because we don\'t want to take the left pointer backwards at any time (e.g. in "abba"), it will only move forward or stay still.\n4. return max {right-left, maxLength}. Doing this outside the loop is essential as it handles strings with all unique chars.\n\n**T/S:** O(n)/O(a), where n = s.length, a = size of character set\n```\npublic int lengthOfLongestSubstring(String s) {\n\tvar left = 0;\n\tvar right = 0;\n\tvar maxLength = 0;\n\n\tfor (var map = new HashMap<Character, Integer>(); right < s.length();) {\n\t\tvar ch = s.charAt(right);\n\t\t\n\t\tif (map.containsKey(ch)) {\n\t\t\tmaxLength = Math.max(maxLength, right - left);\n\t\t\tleft = Math.max(left, map.get(ch) + 1);\n\t\t}\n\t\t\n\t\tmap.put(ch, right++);\n\t}\n\treturn Math.max(maxLength, right - left);\n}\n```\n\nA slight variation using the template for Sliding Window questions that can be used in:\n* https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters (P)\n* https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters (P)\n* https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters\n* https://leetcode.com/problems/fruit-into-baskets\n* https://leetcode.com/problems/minimum-size-subarray-sum\n* https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters\n* https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold\n* https://leetcode.com/problems/distinct-numbers-in-each-subarray\n* https://leetcode.com/problems/diet-plan-performance\n* [1695. Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value/discuss/2141049/Java-or-Sliding-Window-or-Reuse-LC-3)\n\nand various others\n\n\n```\npublic int lengthOfLongestSubstring(String s) {\n\tvar longest = Integer.MIN_VALUE;\n\tvar map = new HashMap<Character, Integer>();\n\n\tfor (int right = 0, left = 0, distinctChars = 0; right < s.length(); right++) {\n\t\tvar rightChar = s.charAt(right);\n\t\tmap.compute(rightChar, (k, v) -> v == null ? 1 : v + 1);\n\n\t\tif (map.get(rightChar) == 1) {\n\t\t\t// if this char is the first one in the window, update records \n\t\t\tlongest = Math.max(longest, right - left + 1);\n\t\t\tdistinctChars++;\n\t\t} else {\n\t\t    // shrink window until window size becomes equal to number of distinct characters in it, i.e. all chars in the window are distinct\n\t\t\twhile (distinctChars != right - left + 1) {\n\t\t\t\tvar leftChar = s.charAt(left++);\n\t\t\t\tmap.put(leftChar, map.get(leftChar) - 1);\n\t\t\t\tif (map.get(leftChar) == 0)\n\t\t\t\t\tdistinctChars--;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn longest == Integer.MIN_VALUE ? 0 : longest;\n}\n```\n***Please upvote if this helps***
['Two Pointers', 'Sliding Window', 'Java']