# Longest Substring Without Repeating Characters

{% embed url="<https://leetcode.com/problems/longest-substring-without-repeating-characters>" %}

### Problem

> Given a string `s`, find the length of the **longest** **substring** without repeating characters.
>
> &#x20;
>
> **Example 1:**
>
> <pre><code>Input: s = "abcabcbb"
> <strong>Output: 3
> </strong><strong>Explanation: The answer is "abc", with the length of 3.
> </strong></code></pre>
>
> **Example 2:**
>
> <pre><code>Input: s = "bbbbb"
> <strong>Output: 1
> </strong><strong>Explanation: The answer is "b", with the length of 1.
> </strong></code></pre>
>
> **Example 3:**
>
> <pre data-overflow="wrap"><code>Input: s = "pwwkew"
> <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>

### Pseudocode

{% code overflow="wrap" %}

```
- iterate through string to add into a Set, using two pointers
    - one to iterate through loop
    - one as a trailing pointer to delete all letters in the set when repeated letters are found
- if letter found to be repeating/not unique
    - delete trailing letters using a second pointer in a while loop
    - increment pointer until all letters are deleted
    - repeated letter is added into set in the following sequence
- if letter is unique
    - add into set
    - set size = longest unique substring
    - use Math.max() to book keep

```

{% endcode %}

### Solution

```javascript
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
var lengthOfLongestSubstring = function (s) {
  let maxLength = 0;
  let set = new Set();
  let left = 0;

  for (let i = 0; i < s.length; i++) {
    let curr = s[i];

    while (set.has(curr)) {
      set.delete(s[left]);
      left++;
    }

    set.add(curr);
    maxLength = Math.max(maxLength, set.size);
  }

  return maxLength;
};

```

### Time and Space Complexity

#### Time

* What did the code do
* Total -

#### Space

* What did the code do
* Total -


---

# 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://grind75-notes.gitbook.io/notes/week-3/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.
