Minimum K such that every substring of length at least K contains a character c | Set | Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the string S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update string S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Given Input ; Function Call
function MinimumLengthSubstring ( S , N ) { let ans = N ; S = " " + S + " " ; S = S . split ( " " ) for ( let c = ' ' . charCodeAt ( 0 ) ; c <= ' ' . charCodeAt ( 0 ) ; ++ c ) { let prev = 0 ; let max_len = 0 ; S [ 0 ] = String . fromCharCode ( c ) ; S [ N + 1 ] = String . fromCharCode ( c ) ; for ( let i = 1 ; i <= N + 1 ; ++ i ) { if ( S [ i ] == String . fromCharCode ( c ) ) { let len = i - prev ; max_len = Math . max ( max_len , len ) ; prev = i ; } } ans = Math . min ( ans , max_len ) ; } return ans ; } let S = " " ; let N = S . length ; document . write ( MinimumLengthSubstring ( S , N ) ) ;