topic
Given a string, find the length of the longest substring that does not contain repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: Since the longest substring without repeating characters is "abc", its length is 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: Since the longest substring without repeating characters is "b", its length is 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Example 4:
Input: s = ""
Output: 0
Hint:
0 <= s.length <= 5 * 104
s consists of English letters, numbers, symbols and spaces
answer
code
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let start = 0,end = 0;
let mp = new Map();
let ans = 0;
while(end<s.length)
{
let pos = mp.get(s[end]);
if(mp.has(s[end])&&pos>=start)
{
start = mp.get(s[end])+1;
}
mp.set(s[end],end);
ans = Math.max(ans,end-start+1);
end++;
}
return ans;
};