C++/Java/Python/JavaScript || ✅✅Sliding Window || ✅🔥🚀100% Solution Explained
Intuition:\n\nThe problem requires us to find the length of the longest substring without repeating characters in a given string. We can use a sliding window te
# Intuition:\n\nThe problem requires us to find the length of the longest substring without repeating characters in a given string. We can use a sliding window technique to keep track of the longest substring without repeating characters. \n\n# Approach:\n1. Initialize ans=0, l=0, r=0.\n2. Create a map of size 256 to store the last occurrence of each character in the string, initialized with -1.\n3. Traverse the string from left to right, with the right end of the window (r) expanding as long as there are no repeating characters.\n4. When a repeating character is found at index r, we update the left end of the window (l) to the next index of the repeated character. \n5. Update the map with the current index of the repeated character.\n6. Update ans with the maximum length of the current substring and the previous maximum.\n7. Return ans.\n\n# Complexity:\n- Time complexity: The algorithm traverses the given string only once. The time complexity is O(n), where n is the length of the string.\n- Space complexity: The space required to store the map is O(256), which is constant. Therefore, the space complexity is O(1).\n\n---\n# C++\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        int ans=0,l=0,r=0;\n        int  n =s.size();\n        vector<int> map(256,-1);\n        while(r<n){\n            if(map[s[r]]!=-1){\n                l=max(map[s[r]]+1,l);\n            }\n            map[s[r]]=r;\n            ans= max(ans,r-l+1);\n            r++;\n        }\n        return ans;\n    }\n};\n```\n\n---\n# JAVA\n```\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        int ans = 0, l = 0, r = 0;\n        int n = s.length();\n        int[] map = new int[256];\n        Arrays.fill(map, -1);\n        while (r < n) {\n            if (map[s.charAt(r)] != -1) {\n                l = Math.max(map[s.charAt(r)] + 1, l);\n            }\n            map[s.charAt(r)] = r;\n            ans = Math.max(ans, r - l + 1);\n            r++;\n        }\n        return ans;\n    }\n}\n\n```\n\n---\n\n# Python\n```\nclass Solution(object):\n    def lengthOfLongestSubstring(self, s):\n        ans, l, r = 0, 0, 0\n        n = len(s)\n        map = [-1] * 256\n        while r < n:\n            if map[ord(s[r])] != -1:\n                l = max(map[ord(s[r])] + 1, l)\n            map[ord(s[r])] = r\n            ans = max(ans, r - l + 1)\n            r += 1\n        return ans\n\n```\n---\n\n# JavaScript\n```\nvar lengthOfLongestSubstring = function(s) {\n    let ans = 0, l = 0, r = 0;\n    let n = s.length;\n    let map = new Array(256).fill(-1);\n    while (r < n) {\n        if (map[s.charCodeAt(r)] != -1) {\n            l = Math.max(map[s.charCodeAt(r)] + 1, l);\n        }\n        map[s.charCodeAt(r)] = r;\n        ans = Math.max(ans, r - l + 1);\n        r++;\n    }\n    return ans;\n};\n```
['Sliding Window', 'Python', 'C++', 'Java', 'JavaScript']