Length of the smallest substring which contains all vowels | Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current subString ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid subString ; Driver code
function get_index ( ch ) { if ( ch == ' ' ) return 0 ; else if ( ch == ' ' ) return 1 ; else if ( ch == ' ' ) return 2 ; else if ( ch == ' ' ) return 3 ; else if ( ch == ' ' ) return 4 ; else return - 1 ; } function findMinLength ( s ) { let n = s . length ; let ans = n + 1 ; let start = 0 ; let count = new Array ( 5 ) ; for ( let i = 0 ; i < 5 ; i ++ ) { count [ i ] = 0 ; } for ( let x = 0 ; x < n ; x ++ ) { let idx = get_index ( s [ x ] ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } let idx_start = get_index ( s [ start ] ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s [ start ] ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } let s = " " ; document . write ( findMinLength ( s ) ) ;