Length of the longest substring without repeating characters | This function returns true if all characters in str [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code
function areDistinct ( str , i , j ) { var visited = new [ 26 ] ; for ( var k = i ; k <= j ; k ++ ) { if ( visited [ str . charAt ( k ) - ' ' ] == true ) return false ; visited [ str . charAt ( k ) - ' ' ] = true ; } return true ; } function longestUniqueSubsttr ( str ) { var n = str . length ( ) ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i ; j < n ; j ++ ) if ( areDistinct ( str , i , j ) ) res = Math . max ( res , j - i + 1 ) ; return res ; } var str = " " ; document . write ( " " + str ) ; var len = longestUniqueSubsttr ( str ) ; document . write ( " " + " " + " " + len ) ;