Length of the smallest sub | Javascript program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's  count with 0 ; Increase the count in array if a character is found ; Size of given string ; Find maximum distinct characters in any string ; Result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Input string
let NO_OF_CHARS = 256 ; function max_distinct_char ( str , n ) { let count = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { count [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } let max_distinct = 0 ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } function smallesteSubstr_maxDistictChar ( str ) { let n = str . length ; let max_distinct = max_distinct_char ( str , n ) ; let minl = n ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { let subs = null ; if ( i < j ) subs = str . substring ( i , j ) ; else subs = str . substring ( j , i ) ; let subs_lenght = subs . length ; let sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } let str = " " ; let len = smallesteSubstr_maxDistictChar ( str ) ; document . write ( " " + " " + " " + len ) ;