Length of the smallest substring which contains all vowels | JavaScript Program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
function findMinLength ( s ) { var n = s . length ; var counts = new Map ( ) ; var indices = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { if ( counts . has ( s [ i ] ) ) counts . set ( s [ i ] , counts . get ( s [ i ] ) + 1 ) else counts . set ( s [ i ] , 1 ) indices . push ( i ) ; } } if ( counts . size < 5 ) return - 1 ; var flag1 = 0 , flag2 = 0 ; var i = 0 ; var j = indices . length - 1 ; while ( ( j - i ) >= 4 ) { if ( ! flag1 && counts . get ( s [ indices [ i ] ] ) > 1 ) { if ( counts . has ( s [ indices [ i ] ] ) ) counts . set ( s [ indices [ i ] ] , counts . get ( s [ indices [ i ] ] ) - 1 ) i ++ ; } else flag1 = 1 ; if ( ! flag2 && counts . get ( s [ indices [ j ] ] ) > 1 ) { if ( counts . has ( s [ indices [ j ] ] ) ) counts . set ( s [ indices [ j ] ] , counts . get ( s [ indices [ j ] ] ) - 1 ) j -- ; } else flag2 = 1 ; if ( flag1 && flag2 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } var s = " " ; document . write ( findMinLength ( s ) ) ;