[Fastest Solution Explained][0ms][100%] O(n)time complexity O(n)space complexity
\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care brother, 
\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\nThe best result for the code below is ***0ms / 3.27MB*** (beats 99.04% / 90.42%).\n* *** Java ***\n\n```\n\npublic int lengthOfLongestSubstring(String s) {\n    int i = 0, j = 0, max = 0;\n    Set<Character> set = new HashSet<>();\n    \n    while (j < s.length()) {\n        if (!set.contains(s.charAt(j))) {\n            set.add(s.charAt(j++));\n            max = Math.max(max, set.size());\n        } else {\n            set.remove(s.charAt(i++));\n        }\n    }\n    \n    return max;\n}\n\n```\nThe idea is use a hash set to track the longest substring without repeating characters so far, use a fast pointer j to see if character j is in the hash set or not, if not, great, add it to the hash set, move j forward and update the max length, otherwise, delete from the head by using a slow pointer i until we can put character j to the hash set.\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 10MB*** (beats 100.00% / 95.49%).\n* *** Python ***\n\n```\nSliding window\nWe use a dictionary to store the character as the key, the last appear index has been seen so far as value.\nseen[charactor] = index\n\nmove the pointer when you met a repeated character in your window.\n```\n\n```\n\nindext    0    1    2    3   4   5   6   7\nstring    a    c    b    d   b   a   c   d\n          ^                  ^\n          |                  |\n\t\tleft               right\n\t\tseen = {a : 0, c : 1, b : 2, d: 3} \n\t\t# case 1: seen[b] = 2, current window  is s[0:4] , \n\t\t#        b is inside current window, seen[b] = 2 > left = 0. Move left pointer to seen[b] + 1 = 3\n\t\tseen = {a : 0, c : 1, b : 4, d: 3} \nindext    0    1    2    3   4   5   6   7\nstring    a    c    b    d   b   a   c   d\n\t\t\t\t\t\t ^   ^\n\t\t\t\t\t     |   |\n\t\t\t\t      left  right\t\t\nindext    0    1    2    3   4   5   6   7\nstring    a    c    b    d   b   a   c   d\n\t\t\t\t\t     ^       ^\n\t\t\t\t\t     |       |\n\t\t\t\t       left    right\t\t\n\t\t# case 2: seen[a] = 0,which means a not in current window s[3:5] , since seen[a] = 0 < left = 3 \n\t\t# we can keep moving right pointer.\n\n```\n\n```\nclass Solution:\n    def lengthOfLongestSubstring(self, s: str) -> int:\n        seen = {}\n        l = 0\n        output = 0\n        for r in range(len(s)):\n            """\n            If s[r] not in seen, we can keep increasing the window size by moving right pointer\n            """\n            if s[r] not in seen:\n                output = max(output,r-l+1)\n            """\n            There are two cases if s[r] in seen:\n            case1: s[r] is inside the current window, we need to change the window by moving left pointer to seen[s[r]] + 1.\n            case2: s[r] is not inside the current window, we can keep increase the window\n            """\n            else:\n                if seen[s[r]] < l:\n                    output = max(output,r-l+1)\n                else:\n                    l = seen[s[r]] + 1\n            seen[s[r]] = r\n        return output\n```\n* Time complexity :O(n).\nn is the length of the input string.\nIt will iterate n times to get the result.\n\n* Space complexity: O(m)\nm is the number of unique characters of the input.\nWe need a dictionary to store unique characters.\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 23.7MB*** (beats 59.24% / 60.42%).\n* *** C++ ***\n\n\n```\n\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        \n        //SLIDING WINDOW  - TIME COMPLEXITY O(2n)\n        //                  SPACE COMPLEXITY O(m)   //size of array\n        \n        int store[256]={0}; //array to store the occurences of all the characters\n        int l=0;    //left pointer\n        int r=0;    //right pointer\n        int ans=0;  //initializing the required length as 0\n        \n        while(r<s.length())     //iterate over the string till the right pointer reaches the end of the string \n        {\n            store[s[r]]++;      //increment the count of the character present in the right pointer \n            \n            while(store[s[r]]>1)    //if the occurence become more than 1 means the char is repeated\n            { \n                store[s[l]]--;   //reduce the occurence of temp as it might be present ahead also in the string\n                l++;         //contraction of the present window till the occurence of the \'t\' char becomes 1\n            }\n            \n            ans = max(ans,r-l+1);    //As the index starts from 0 , ans will be (right pointer-left pointer + 1)\n            r++;        // now will increment the right pointer \n        }\n        return ans;\n    }\n};\n\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 78MB*** (beats 100.00% / 100.00%).\n* *** JavaScript ***\n\n\n```\n\nfunction lengthOfLongestSubstring(s) {\n    const map = {};\n    var left = 0;\n    \n    return s.split(\'\').reduce((max, v, i) => {\n        left = map[v] >= left ? map[v] + 1 : left;\n        map[v] = i;\n        return Math.max(max, i - left + 1);\n    }, 0);\n}\n\n```\n\n```\nTime Complexity = O(N)\nSpace Complexity = O(N)\n```\n\n```\nvar lengthOfLongestSubstring = function(s) {\n    // keeps track of the most recent index of each letter.\n    const seen = new Map();\n    // keeps track of the starting index of the current substring.\n    let start = 0;\n    // keeps track of the maximum substring length.\n    let maxLen = 0;\n    \n    for(let i = 0; i < s.length; i++) {\n        // if the current char was seen, move the start to (1 + the last index of this char)\n        // max prevents moving backward, \'start\' can only move forward\n        if(seen.has(s[i])) start = Math.max(seen.get(s[i]) + 1, start)\n        seen.set(s[i], i);\n        // maximum of the current substring length and maxLen\n        maxLen = Math.max(i - start + 1, maxLen);\n    } \n    \n    return maxLen;  \n};\n```\n\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 33.33MB*** (beats 99.00% / 60.12%).\n* *** Kotlin ***\n\n\n```\n\nfun lengthOfLongestSubstring(s: String): Int {\n    var maxLength = 0\n    val queue = LinkedList<Char>()\n    for (i in s.indices) {\n        if (queue.isNotEmpty()) {\n            when {\n                queue.first == s[i] -> queue.poll()\n                queue.last == s[i] -> queue.clear()\n                queue.contains(s[i]) -> {\n                    while (queue.isNotEmpty()) {\n                        if (queue.poll() == s[i]) break\n                    }\n                }\n            }\n        }\n\n        maxLength = max(maxLength, queue.size+1)\n        queue.offer(s[i])\n    }\n\n    return maxLength\n}\n```\n\n```\nimport kotlin.math.*\n\nclass Solution {\n    private val hashMap = mutableMapOf<Char, Int>()\n    private var longest = Pair<Int, Int>(0, 1)\n    \n    fun lengthOfLongestSubstring(s: String): Int {\n        if (s.length == 0) {\n            return 0\n        }\n        \n        var start = 0\n        \n        for (i in 0 until s.length) {\n            val letter = s[i]\n            if (hashMap.containsKey(letter)) {\n                // start = max(start, hashMap[letter]!! + 1)\n                // why use max() ? "abba": if we take not max, then when visiting second \'a\' ->\n                // we\'ll take b, but don\'t need it as it\'ll include duplicate\n                start = hashMap[letter]!! + 1\n                // +1 as we don\'t need current that has duplication, but next letter after it\n            }\n            val (firstIdx: Int, secondIdx: Int) = longest\n            \n            if (secondIdx - firstIdx < i - start + 1) {\n                longest = Pair<Int, Int>(start, i+1)\n                // +1 is to include current last letter as otherwise it will be excluding\n            }\n            hashMap.put(letter, i)\n        }\n        val (firstIdx: Int, secondIdx: Int) = longest\n        \n        return secondIdx - firstIdx\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 13.17MB*** (beats 79.34% / 99.92%).\n* *** Swift ***\n\n\n```\n\nclass Solution {\n    func lengthOfLongestSubstring(_ s: String) -> Int {\n        guard !s.isEmpty else { return 0 }\n        var len = 0, chars = [Character]()\n        for c in s {\n            if let idx = chars.firstIndex(of: c) {\n                chars.removeSubrange(0...idx)\n            }\n            chars.append(c)\n            len = max(len, chars.count)\n        }\n        return len\n    }\n}\n\n```\n* O(N)\n```\nclass Solution {\n    func lengthOfLongestSubstring(_ s: String) -> Int {\n        var longest = 0, startIndex = 0\n        var charMap: [Character: Int] = [:]\n\n        for (index, char) in s.enumerated() {\n            if let foundIndex = charMap[char] {\n                startIndex = max(foundIndex+1, startIndex)\n            }\n            longest = max(longest, index - startIndex + 1)\n            charMap[char] = index\n        }\n        return longest\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 62.07MB*** (beats 99.99% / 99.99%).\n* *** PHP ***\n\n\n```\n\nclass Solution {\n\n/**\n * @param String $s\n * @return Integer\n */\nfunction lengthOfLongestSubstring($s) {\n\t$start = 0;    //current starting position of search\n    $length = 0; //current max length of substring\n    for($i = 0; $i < strlen($s); $i++){\n        $char = $s[$i];\n        if(isset($arr[$char]) && $arr[$char] >= $start){\n            $start = $arr[$char] + 1;\n        } elseif($i - $start === $length) {\n            $length++;\n        }\n        $arr[$char] = $i;\n    }\n    return $length;\n}\n}\n\n```\n\n```\nclass Solution {\n\n    /**\n     * @param String $s\n     * @return Integer\n     */\n    function lengthOfLongestSubstring($s) \n    {\n        if (strlen($s) === 0) return 0;\n        if (strlen($s) === 1) return 1;\n        \n        $chars = str_split($s);\n        \n        $i = $j = $max = 0;\n        $seen = [];\n        \n        while ($i < count($chars))\n        {\n            $c = $chars[$i];\n           \n            while (array_key_exists($c, $seen))\n            {\n                unset($seen[$chars[$j]]);\n        \t\t$j++;\n            }\n            \n            $seen[$chars[$i]] = true;\n            \n            $max = max($i - $j + 1, $max);\n            $i++;\n        }\n        return $max;\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 1.17MB*** (beats 99.64% / 99.92%).\n* *** C ***\n\n\n```\n\nint lengthOfLongestSubstring(char * s){\n    /*letter_map is to keep track if we saw this character in this substring*/\n    int letter_map[128] = {0}, res = 0;\n    char* start = s, *end = s;\n    \n    while (*end) {\n        /* If we reached a letter we saw already - check max length and start a new substring*/\n        if (letter_map[*end]) {\n            res = (end - start > res) ? end - start : res;\n            while (*start != *end) {\n                letter_map[*start] = 0;\n                start++;\n            }\n            start++;\n            end++;\n        }\n        else {\n            letter_map[*end] = 1;\n            end++;\n        }  \n    }\n    return end-start > res ? end-start : res;\n}\n\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***\n
['Swift', 'C', 'PHP', 'Python', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript']