Given a string, find the length of the longest substring without repeating characters.

Note
that the answer must be a substring, "pwke" is a subsequence and not a substring.

Example 1:

 123 Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. 

Example 2:

 123 Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. 

Example 3:

 123 Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. 

Related Topics: Hash TableString

## 解題邏輯與實作

### Sliding Window

1. 初始化一個 HashMap indexes ，用以記錄目前出現過的字元，與其對應的下標。

2. 開始輪巡整個字串，每一輪讀入一個新的字元，若該字元存於 indexes，則判斷記錄的下標是大於 start ，若大於 start 則更新 start 所指向的位置。

3. 每一輪結束時，記錄最長的長度並更新新字元於 indexes 所記錄的下標。

 1234567891011121314 class Solution: def lengthOfLongestSubstring(self, s: str) -> int: start = 0 max_len = 0 indexes = {} for idx, letter in enumerate(list(s)): if letter in indexes and start <= indexes[letter]: start = indexes[letter] + 1 max_len = max(idx-start+1, max_len) indexes[letter] = idx return max_len