# Longest Substring Without Repeating Characters

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

{% tabs %}
{% tab title="Scala" %}

```scala
import scala.collection.mutable.HashMap

def lengthOfLongestSubstring(s: String): Int = {
  var map = new HashMap[Char, Int]
  var j, k, length = 0
  for ((c, i) <- s.zipWithIndex) {
    k = map.get(c).getOrElse(j)
    j = j max k
    length = length max i - j
    map += (c -> i)
  }
  length
}

lengthOfLongestSubstring("")
lengthOfLongestSubstring("pwwkew")
lengthOfLongestSubstring("bbbbb")
lengthOfLongestSubstring("abcabcbb")
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# 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://blog.bernardw.com/problems/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.
