> For the complete documentation index, see [llms.txt](https://chunhthanhde.gitbook.io/leetcode-top-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-3-sliding-window/031-longest-substring-without-repeating-characters.md).

# 3. Longest Substring Without Repeating Characters 🌐

**Difficulty**: `Medium` - **Tags**: `Hash Table`, `String`, `Sliding Window`

[LeetCode Problem Link](https://leetcode.com/problems/longest-substring-without-repeating-characters/)

## Problem Statement 📜

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

## Examples 🌟

🔹 **Example 1:**

**Input:**

```python
s = "abcabcbb"
```

**Output:**

```
3
```

**Explanation:** The answer is "abc", with the length of 3.

🔹 **Example 2:**

**Input:**

```python
s = "bbbbb"
```

**Output:**

```
1
```

**Explanation:** The answer is "b", with the length of 1.

🔹 **Example 3:**

**Input:**

```python
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.

## Constraints ⚙️

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

## Solution 💡

To solve this problem, we can use the **Sliding Window** approach:

1. **Use a Set**: Utilize a `HashSet` to track characters in the current window.
2. **Expand and Shrink the Window**:
   * Expand the `right` pointer to include characters in the window.
   * If a duplicate character is found, shrink the window from the left until the window contains no duplicate characters.
3. **Update the Result**:
   * Keep track of the maximum length of valid substrings.

### Java Solution

```java
import java.util.HashSet;

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashSet<Character> charSet = new HashSet<>();
        int left = 0, maxLength = 0;

        for (int right = 0; right < s.length(); right++) {
            while (charSet.contains(s.charAt(right))) {
                charSet.remove(s.charAt(left));
                left++;
            }
            charSet.add(s.charAt(right));
            maxLength = Math.max(maxLength, right - left + 1);
        }

        return maxLength;
    }
}
```

## Time Complexity ⏳

* **O(n)**: Each character is processed at most twice (once added, once removed).

## Space Complexity 💾

* **O(min(n, m))**: Space used by the `HashSet`, bounded by the size of the input `n` and the size of the character set `m`.

You can find the full solution [here](https://github.com/ChunhThanhDe/Leetcode-Top-Interview/blob/main/Topic%203%20Sliding%20Window/031%20Longest%20Substring%20Without%20Repeating%20Characters/Solution.java).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://chunhthanhde.gitbook.io/leetcode-top-interview/topic-3-sliding-window/031-longest-substring-without-repeating-characters.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.
