# Longest Substring Without Repeating Characters LeetCode Solution

Here, We see Longest Substring Without Repeating Characters problem Solution. This Leetcode problem done in many programming language like C++, Java, JavaScript, Python etc. with different approach.

## Problem Statement ->

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

```Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:
Input: 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.```

## C++ Solution ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = int(s.length()), res = 0;
unordered_map<char, int> mp;

for (int j = 0, i = 0; j < n; j++){
if(mp[s[j]] > 0) {
i = max(mp[s[j]], i);
}
res = max(res, j - i + 1);
mp[s[j]] = j + 1;
}
return res;
}
};
```Code language: C++ (cpp)```

## Java Solution ->

``````public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}
```Code language: Java (java)```

## Python Solution ->

``````class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
ans = 0
# mp stores the current index of a character
mp = {}

i = 0
# try to extend the range [i, j]
for j in range(n):
if s[j] in mp:
i = max(mp[s[j]], i)

ans = max(ans, j - i + 1)
mp[s[j]] = j + 1

return ans
```Code language: Python (python)```

Longest Substring Without Repeating Characters python solution, Longest Substring Without Repeating Characters java solution, Longest Substring Without Repeating Characters JavaScript solution, Longest Substring Without Repeating Characters C++ solution

#### Add Two Numbers LeetCode Solution

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order,…

#### Best Time to Buy and Sell Stock II

Here, We see Best Time to Buy and Sell Stock II problem Solution. This Leetcode problem…

#### Zigzag Conversion LeetCode Solution

The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows…

#### Two Sum LeetCode Solution

Given an array of integers nums and an integer target, return indices of the two…

#### Median of Two Sorted Arrays LeetCode Solution

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time…

#### Remove Duplicates from Sorted Array

Here, We see Remove Duplicates from Sorted Array problem Solution. This Leetcode problem done in…