# 3. Leetcode Python: Longest Substring Without Repeating Characters - Medium

### Problem

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

**Example 1:**

<pre><code><strong>Input: s = "abcabcbb"
</strong><strong>Output: 3
</strong><strong>Explanation: The answer is "abc", with the length of 3.
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: s = "bbbbb"
</strong><strong>Output: 1
</strong><strong>Explanation: The answer is "b", with the length of 1.
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: s = "pwwkew"
</strong><strong>Output: 3
</strong><strong>Explanation: The answer is "wke", with the length of 3.
</strong>Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
</code></pre>

**Constraints:**

* `0 <= s.length <= 5 * 104`
* `s` consists of English letters, digits, symbols and spaces.

### Thoughts <a href="#thoughts" id="thoughts"></a>

The solution uses a sliding window technique to keep track of the longest substring without repeating characters. A set is used to keep track of all the characters seen in the window.

The algorithm iterates through each character in the input string, s. For each character, it checks to see if it has been seen before. If it has not been seen, it is added to the set and the current window is expanded. If the character has been seen before, then the start of the window is adjusted so that the current window contains only characters that have not been seen before.

The maximum length of the substring is calculated and updated each time the window is expanded. The maximum length is then returned once all characters have been processed.

### Solution <a href="#thoughts" id="thoughts"></a>

Time Complexity: O(N)\
Space Complexity: O(N)

```
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        n = len(s) 
        
        # Set of all characters seen 
        char_set = set() 
    
        # Initialize max_length 
        max_length = 0 
    
        # Current window 
        start = 0 
        end = 0 
    
        while (end < n): 
            curr_char = s[end] 
    
            # If the current window does not contain  
            # duplicate characters 
            if curr_char not in char_set: 
                char_set.add(s[end]) 
                end += 1 
                max_length = max(max_length, end - start) 
    
            # If the current window contains duplicate characters 
            else: 
                char_set.remove(s[start]) 
                start += 1 
    
        return max_length
        
def main():
    solution = Solution()
    
    # Test Cases
    assert solution.lengthOfLongestSubstring('abcabcbb') == 3
    assert solution.lengthOfLongestSubstring('aaaa') == 1
    assert solution.lengthOfLongestSubstring('pwwkew') == 3
    
if __name__ == "__main__":
    main()

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helfipangestu.gitbook.io/leetcode-solutions/solution-1-50/3.-leetcode-python-longest-substring-without-repeating-characters-medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
