/ * *

The title

Given a string, find the length of the smallest string that does not contain repeating characters.

Example 1:

Input: s = “abcabcbb” Output: 3 Explanation: Since the oldest string without repeating characters is “ABC”, its length is 3. Example 2:

Input: s = “BBBBB” Output: 1 Explanation: Since the oldest string without repeating characters is “b”, its length is 1. Example 3:

Input: s = “pwwkew” Output: 3 Explanation: Since the oldest string without repeating characters is “wKE”, its length is 3. Note that your answer must be the length of the substring, “pwke” is a subsequence, not a substring. Example 4:

Input: s = “output: 0

Tip:

0 <= s.length <= 5 * 104 s The value consists of letters, digits, symbols, and Spaces

Source: LeetCode link: leetcode-cn.com/problems/lo… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

print(lengthOfLongestSubstring(“pwwkew”))

notes

So the idea of this sliding window problem is to maintain the correctness of the window and make sure that the data in the window is not repeated and then take the historical maximum value of the window throughout the cycle

The code address

Github.com/zmfflying/Z… * /

The problem solving code

Import Foundation func lengthOfLongestSubstring(_ s: String) -> Int {var res: Int = 0 [Character] = [Character]() for c in Array(s) {// Let index = path.firstIndex(of: c) { path.removeSubrange(Range.init(NSMakeRange(0, index+1))!) } path.append(c) // Set the value of res = Max (res, Path.count)} return res} //func lengthOfLongestSubstring(_ s: String) -> Int {// var res: Int = 0 // var dic: [Character: Int] = [Character: Int]() // var start: Int = 0 // let arr = Array(s) // var end: Int = 0 // let count = arr.count // // while end < count && start < count { // let c = arr[end] // if dic[c] ! = nil { // start = max(start, dic[c]!) // } // end += 1 // res = max(res, end - start) // dic[c] = end // } // // return res //}Copy the code