Length of the smallest substring which contains all vowels | Java 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
import java . util . * ; class GFG { public static int findMinLength ( String s ) { int n = s . length ( ) ; HashMap < Character , Integer > counts = new HashMap < > ( ) ; Vector < Integer > indices = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' || s . charAt ( i ) == ' e ' || s . charAt ( i ) == ' o ' || s . charAt ( i ) == ' i ' || s . charAt ( i ) == ' u ' ) { if ( counts . containsKey ( s . charAt ( i ) ) ) { counts . replace ( s . charAt ( i ) , counts . get ( s . charAt ( i ) ) + 1 ) ; } else { counts . put ( s . charAt ( i ) , 1 ) ; } indices . add ( i ) ; } } if ( counts . size ( ) < 5 ) return - 1 ; int flag1 = 0 , flag2 = 0 ; int i = 0 ; int j = indices . size ( ) - 1 ; while ( ( j - i ) >= 4 ) { if ( flag1 == 0 && counts . get ( s . charAt ( indices . get ( i ) ) ) > 1 ) { counts . replace ( s . charAt ( indices . get ( i ) ) , counts . get ( s . charAt ( indices . get ( i ) ) ) - 1 ) ; i ++ ; } else flag1 = 1 ; if ( flag2 == 0 && counts . get ( s . charAt ( indices . get ( j ) ) ) > 1 ) { counts . replace ( s . charAt ( indices . get ( j ) ) , counts . get ( s . charAt ( indices . get ( j ) ) ) - 1 ) ; j -- ; } else flag2 = 1 ; if ( flag1 == 1 && flag2 == 1 ) break ; } return ( indices . get ( j ) - indices . get ( i ) + 1 ) ; } public static void main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; System . out . print ( findMinLength ( s ) ) ; } }