Input: s ="abcabcbb" Output: 3 Explanation: The answer is"abc", with the length of 3.
Example 2:
1 2 3
Input: s ="bbbbb" Output: 1 Explanation: The answer is"b", with the length of 1.
Example 3:
1 2 3 4
Input: s ="pwwkew" Output: 3 Explanation: The answer is"wke", with the length of 3. Notice that the answer must be a substring, "pwke"is a subsequence and not a substring.
funclengthOfLongestSubstring(_s: String) -> Int { // The border situation, it is important if s.isEmpty { return0 } var result =1// maximum length of substring without repeating characters var begin =0// starting index of current substring var walkDict = [Character: Int]() // hashtable to store each character's index for (i, ch) in s.enumerated() { // loop through each character in string s if (walkDict[ch] !=nil) && walkDict[ch]!>= begin && walkDict[ch]!>-1 { // if current character is already in hashtable and it's index is greater than or equal to the starting index // of current substring and not -1, update the starting index of current substring begin = walkDict[ch]!+1 walkDict[ch] = i // update the hashtable with current character's index } else { walkDict[ch] = i // add current character's index to hashtable result =max(result, i - begin +1) // update the maximum length of substring without repeating characters } } return result }