304 North Cardinal St.
Dorchester Center, MA 02124

# Longest Substring Without Repeating Characters LeetCode Solution

## Problem – Longest Substring Without Repeating Characters LeetCode Solution

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

Constraints:

• `0 <= s.length <= 5 * 104`
• `s` consists of English letters, digits, symbols and spaces.

### Longest Substring Without Repeating Characters LeetCode Solution in Java

``````   public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}
``````

### Longest Substring Without Repeating Characters LeetCode Solution in C++

``````int lengthOfLongestSubstring(string s) {
vector<int> dict(256, -1);
int maxLen = 0, start = -1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
``````

### Longest Substring Without Repeating Characters LeetCode Solution in Python

``````class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
start = maxLength = 0
usedChar = {}

for i in range(len(s)):
if s[i] in usedChar and start <= usedChar[s[i]]:
start = usedChar[s[i]] + 1
else:
maxLength = max(maxLength, i - start + 1)

usedChar[s[i]] = i

return maxLength
``````

### Longest Substring Without Repeating Characters LeetCode Solution in JavaScript

``````function lengthOfLongestSubstring(s) {
const map = {};
var left = 0;

return s.split('').reduce((max, v, i) => {
left = map[v] >= left ? map[v] + 1 : left;
map[v] = i;
return Math.max(max, i - left + 1);
}, 0);
}
``````

### Longest Substring Without Repeating Characters LeetCode Solution in Swift

``````class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
guard !s.isEmpty else { return 0 }
var len = 0, chars: [Character] = []
for c in s {
if let idx = chars.firstIndex(of: c) {
chars.removeSubrange(0...idx)
}
chars.append(c)
len = max(len, chars.count)
}
return len
}
}
``````
##### Longest Substring Without Repeating Characters LeetCode Solution Review:

In our experience, we suggest you solve this Longest Substring Without Repeating Characters LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Longest Substring Without Repeating Characters LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Longest Substring Without Repeating Characters LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions