Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #longest #substring #repeating #characters #javascript
ADD COMMENT
Topic
Name
8+4 =