Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

longest substring without repeating characters in javascript

function lengthOfLongestSubstring(string) {
    var max = 0, current_string = "", i, char, pos;

    for (i = 0; i < string.length; i += 1) {
        char = string.charAt(i);
        pos = current_string.indexOf(char);
        if (pos !== -1) {
            // cut "dv" to "v" when you see another "d"
            current_string = current_string.substr(pos + 1);
        }
        current_string += char;
        max = Math.max(max, current_string.length);
    }
    return max;
}

lengthOfLongestSubstring("dvdf"); // 3
Comment

PREVIOUS NEXT
Code Example
Javascript :: js computed style 
Javascript :: cheerio load 
Javascript :: javascript word start with 
Javascript :: get all keys of object in javascript 
Javascript :: clear location state react 
Javascript :: first x characters of string javascript 
Javascript :: motify react native 
Javascript :: hnazmul 
Javascript :: animate flash jQuery 
Javascript :: stack implementation in javascript using array 
Javascript :: js string slice last n elements 
Javascript :: javascript object to json string 
Javascript :: if checkbox is checked jquery 
Javascript :: javascript check object methods 
Javascript :: how to truncate string in javascript 
Javascript :: radio button default checked react 
Javascript :: vim total number of lines 
Javascript :: Uncaught ReferenceError: function is not defined at HTMLUnknownElement.onclick 
Javascript :: javascript escape html 
Javascript :: delete list of keys from object javascript 
Javascript :: angular display block 
Javascript :: location javascript redirect 
Javascript :: math format comma separated in javascript 
Javascript :: react style ternary operator 
Javascript :: disemvowel trolls codewars javascript 
Javascript :: flatlist listemptycomponent center 
Javascript :: jquery merge objects 
Javascript :: play audio in javascript 
Javascript :: filter falsy values js 
Javascript :: pad js 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =