Used HashSet in JAVA ✅ || Explained Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nif you know sliding window...then it can be intuitive. But if you don\'t know ...no wor
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nif you know sliding window...then it can be intuitive. But if you don\'t know ...no worry i will teach you...\nRefer below approach points.....\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use sliding window with hashset, use left and right pointers to move the window . \n2. If the set doesn\'t contains character then first add into the set and calculate the maxLength hand-in-hand... \n3. if character already present in the set that means you have to move your sliding window by 1 , before that you have to remove all the characters that are infront of the character that is present already in window before.\n4. Now you have to remove that character also and move the left pointer and also add the new character into the set.\n5. THAT\'S ALL........EASY APPROACH USING SIMPLE HASHSET+SLIDING WINDOW\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(k), where k is the number of distinctive characters prsent in the hashset.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        Set<Character>set=new HashSet<>();\n        int maxLength=0;\n        int left=0;\n        for(int right=0;right<s.length();right++){\n           \n            if(!set.contains(s.charAt(right))){\n                set.add(s.charAt(right));\n                maxLength=Math.max(maxLength,right-left+1);\n                \n            }else{\n                while(s.charAt(left)!=s.charAt(right)){\n                    set.remove(s.charAt(left));\n                    left++;\n                }\n                set.remove(s.charAt(left));left++;\n                set.add(s.charAt(right));\n            }\n            \n        }\n        return maxLength;\n    }\n}\n```\n\n![upvote.jpg](https://assets.leetcode.com/users/images/cb663845-f940-4fb1-b1ab-a31012fdacee_1675798037.916439.jpeg)\n\n\n**PLEASE UPVOTE** .....if you find this solution useful , and if any suggestions to improve then please comment.
['Hash Table', 'Sliding Window', 'Python', 'C++', 'Java']