Meta / Amazon / Google / Microsoft| Longest Substring Without Repeating Characters | LeetCode-3: Medium | JavaScript Implementation
Mar 15, 2022
We will be seeing the solution to the LeetCode problem 3 in JavaScript. I hope you will find it useful.
Problem Statement:
Given a string s
, find the length of the longest substring without repeating characters.
Link to Problem: https://leetcode.com/problems/longest-substring-without-repeating-characters
JavaScript Implementation:
var lengthOfLongestSubstring = function(s) {
let length = s.length
if(s==""){ return 0};
let hashMap = {};
let start = 0;
let sub_sequnce_now = 0;
let max_sub_sequnce_now = 0;
for(let i = 0;i<length;i++){
if(hashMap[s[i]] != null && (start<=hashMap[s[i]])){
sub_sequnce_now = sub_sequnce_now-(hashMap[s[i]] - start);
start = hashMap[s[i]]+1
hashMap[s[i]] = i;
}else{
hashMap[s[i]] = i;
sub_sequnce_now=sub_sequnce_now+1;
if(max_sub_sequnce_now<sub_sequnce_now){
max_sub_sequnce_now = sub_sequnce_now
}
}
}
return max_sub_sequnce_now
};
Thank you for reading hope this code is helpful to you.