JS | 98% | Sliding window | With exlanation
\n\nWindow Sliding Technique is a computational technique which aims to reduce the use of nested loop and replace it with a single loop, thereby reducing the ti
![image](https://assets.leetcode.com/users/images/8f8012d5-d9a2-4887-8a8b-3d8670e1c32e_1665574502.5093148.png)\n\n**Window Sliding Technique** is a computational technique which aims to reduce the use of nested loop and replace it with a single loop, thereby reducing the time complexity.\nThe Sliding window technique can reduce the time complexity to **O(n).**\n\n![image](https://assets.leetcode.com/users/images/9d3a36c0-7a7e-4d67-9034-5578ff769cc3_1665574655.3900712.png)\n![image](https://assets.leetcode.com/users/images/2218dd45-66de-4d1c-930d-e8975588b659_1666078768.2626626.png)\n\n\n\nBelow are some basic tips for identifying this kind of problem where we could use the sliding window technique:\n\n* The problem will be based on an array, string, or list data structure.\n* You need to find the subrange in this array or string that should provide the longest, shortest, or target values.\n* A classic problem: to find the largest/smallest sum of given k (for example, three) consecutive numbers in an array.\n\nThe length can be **fixed**, as in the example above, or it can be **dynamic**, just like in this problem.\n\nSo to solve this problem, we can use the hash table and iterate over our term. Let\'s do two checks on 0 and 1 so as not to waste extra time. Now we can start, in the picture above you can see our **window** (blue rectangle) and its movement during the scan. We gradually add our letters to the **hash table**, and if it already contains this letter, we delete the letter corresponding to the leftmost index, do this until we get to the desired one and delete the repetition. Only after that we add our new element. \nThe maximum length is found by comparing our current length with the new one, using **Math.max**.\n\nIn the picture you can see all the movement of the window and the state of our hash table.\n\n\n```\nvar lengthOfLongestSubstring = function (s) {\n    let set = new Set();\n    let left = 0;\n    let maxSize = 0;\n\n    if (s.length === 0) return 0;\n    if (s.length === 1) return 1;\n\n    for (let i = 0; i < s.length; i++) {\n\n        while (set.has(s[i])) {\n            set.delete(s[left])\n            left++;\n        }\n        set.add(s[i]);\n        maxSize = Math.max(maxSize, i - left + 1)\n    }\n    return maxSize;\n}\n```\n\nI hope I was able to explain clearly.\n**Happy coding**! \uD83D\uDE43\n
['Hash Table', 'Sliding Window', 'JavaScript']