JavaScript True O(n) Sliding Window with Explanation
Complexity\n- Time complexity: O(n)\n- Space complexity: O(n)\n\n# Code\n\n/**\n * @param {string} s\n * @return {number}\n */\nfunction lengthOfLongestSubstrin
# Complexity\n- Time complexity: O(n)\n- Space complexity: O(n)\n\n# Code\n```\n/**\n * @param {string} s\n * @return {number}\n */\nfunction lengthOfLongestSubstring(inputString) {\n    if (inputString.length == 0) return 0;\n\n    let n = inputString.length;\n    \n    // starting index of current window substring\n    let stCurr = 0,\n        // length of the longest substring\n        longest = 0,\n        // length of the current substring (size of window)\n        currLen = 0,\n        // starting index of longest substring\n        start = 0;\n\n\t// hashmap to store the element as key and index last seen as value\n    let lastSeenAt = {};\n\n    // Traverse inputString to find the longest substring\n    // without repeating characters.\n    for (index = 0; index < n; index++) {\n        let val = inputString[index];\n\n        // If the current element is not present in the hash map,\n        // then store it in the hash map with the value as the current index.\n        if (!(val in lastSeenAt)) lastSeenAt[val] = index;\n        else {\n            // If the current element is present in the hash map,\n            // it means that this element may have appeared before.\n            // Check if the current element occurs before or after `stCurr`.\n            if (lastSeenAt[val] >= stCurr) {\n                currLen = index - stCurr;\n                if (longest < currLen) {\n                    longest = currLen;\n                    start = stCurr;\n                }\n                // The next substring will start after the last\n                // occurence of the current element.\n                stCurr = lastSeenAt[val] + 1;\n            }\n\n            // Update the last occurence of\n            // the element in the hash map\n            lastSeenAt[val] = index;\n        }\n    }\n\n    // Update the longest substring\'s\n    // length and starting index.\n    if (longest < index - stCurr) {\n        start = stCurr;\n        longest = index - stCurr;\n    }\n\n    return longest;\n};\n```
['JavaScript']