Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param {string} s
- * @return {number}
- */
- var lengthOfLongestSubstring = function(s) {
- if(s.length < 2) return s.length;
- let seenChars = {};
- let startId = 0;
- let maxLength = 0;
- let currLength = 0;
- for(let i = 0; i < s.length; i++){
- const currChar = s[i];
- if(seenChars[currChar] !== undefined) {
- currLength = i - startId;
- if(currLength > maxLength) maxLength = currLength;
- startId = seenChars[currChar] + 1;
- seenChars[currChar] = i;
- const keys = Object.keys(seenChars);
- for(let j = 0; j < keys.length; j++){
- const seenCharValue = seenChars[keys[j]];
- if(seenCharValue < startId) seenChars[keys[j]] = undefined;
- }
- } else {
- seenChars[currChar] = i;
- }
- }
- if(s.length - startId > maxLength) return s.length - startId;
- return maxLength;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement