Swift, faster than 79.38% of swift submissions
O(n)\n\nclass Solution {\n    func lengthOfLongestSubstring(_ s: String) -> Int {\n        var longest = 0, startIndex = 0\n        var charMap: [Character: Int
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```
['Swift']