Swift Time Exceeded Fix
The "optimized" solution still runs over the acceptable time limit when the solution is delivered in Swift. I did a quick profiling session and realized the pro
The "optimized" solution still runs over the acceptable time limit when the solution is delivered in Swift. I did a quick profiling session and realized the problem is in Swift\'s implementation of String and how it accesses information about the underlying characters. \n\n![image](https://assets.leetcode.com/users/vladinecko/image_1557211525.png)\n\nIf you\'re accessing the string to get the current character inside your loop, you will be making a ton of calls to String internals, which are currently super slow. The easiest way to circumvent that without having to convert the string into an array is to use String\'s iterator. Below is my implementation that could be optimized further but is already running faster than ~90% of submissions. \n\n```\nfunc longestSubstringLengthOptimized(_ string: String) -> Int {\n\tguard string.count > 0 else {\n\t\treturn 0\n\t}\n\n\tvar charIndexes = [Character:Int]()\n\tvar i = 0\n\tvar j = 0\n\tvar maxLength = 1\n\n\tfor currentChar in string {\n\t\tif let index = charIndexes[currentChar] {\n\t\t\tmaxLength = max(maxLength, j - i)\n\t\t\ti = max(i, index)\n\t\t}\n\t\tj += 1\n\t\tcharIndexes[currentChar] = j\n\t}\n\n\treturn max(maxLength, j - i)\n}\n```
[]