Udemy JS Algorithms & Data Structures Masterclass- Section 6 Exercise 9 (1st try)
Sliding Window - findLongestSubstring
Write a function called findLongestSubstring, which accepts a string and returns the length of the longest substring with all distinct characters.
My code efficiency: Reset pointer to next element to start count from there when you find a duplicate letter in the string.
//start search from first element of string
//each element after must not have been a previous element
//if duplicate, break out of loop. Move pointer up 1.
//end while loop when pointer is >= string's length
function findLongestSubstring(input){
let pointer = 0;
let point2 = 0;
let count = 0;
let arr = [];
if (!input.length) return 0;
while(pointer < input.length) {
let letter = input.charAt(pointer);
if ( !arr.includes(letter) ) {
arr.push(letter);
if (pointer === input.length - 1 && arr.length > count) count = arr.length;
}
else if (arr.length === "" || arr.length <= count) {
arr = [];
pointer = point2++;
}
else if (arr.length > count) {
count = arr.length;
arr = [];
pointer = point2++;
}
pointer++;
}
return count;
}
Comments
Post a Comment