In this article we will learn about LeetCode Third question Longest Substring Without Repeating Characters in detail with optimized solution with Example.

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

Table of Contents

### Example 1:

**Input: s = “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.**

### Example 2:

**Input: s = “bbbbb”Output: 1Explanation: The answer is “b”, with the length of 1.**

### Example 3:

**Input: s = “pwwkew”Output: 3Explanation: 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<sup>4</sup>`

`s`

consists of English letters, digits, symbols and spaces.

### Java Example

```
class Solution {
public int lengthOfLongestSubstring(String s) {
int l=0,r=0;
Set<Character> seen = new HashSet();
int max=0;
while(r< s.length()){
char c= s.charAt(r);
if(seen.add(c)){
max= Math.max(max,r-l+1);
r++;
}else{
while(s.charAt(l)!=c){
seen.remove(s.charAt(l));
l++;
}
seen.remove(c);
l++;
}
}
return max;
}
}
// 2023
```

### Complexity :

Time complexity: O(N);

Space Complexity: O(1);

I hope you like this Amazing Longest Substring article.🙂

Also Read :