Their thinking

By the properties of the dictionary can determine whether there is any existing characters did not save characters and the corresponding subscript By double marking calculation length 1, two tags are from zero beginning, if there is no the second tag characters go straight length has been increased 2, if a character in a dictionary the position of the first character of the current position, If local has not been reset, perform the +1 operation func lengthOfLongestSubstring(_ s: String) -> Int { var dict = Dictionary<Character, Int>() var count = 0 var length = 0 var local = 0 var isreset = false for char in s { let item = dict[char] if item ! = nil{ if item! >= local { local = item! isreset = true } if count-local > length{ length = count-local } }else{ if ! isreset { if count-local+1 > length{ length = count-local+1 } }else{ if count-local > length{ length = count-local } } }  dict[char] = count count += 1 } return length }Copy the code