Length of the smallest substring which contains all vowels | Java program to find the length of the smallest subString of 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
import java . util . * ; class GFG { static int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return - 1 ; } static int findMinLength ( String s ) { int n = s . length ( ) ; int ans = n + 1 ; int start = 0 ; int count [ ] = new int [ 5 ] ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s . charAt ( x ) ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s . charAt ( 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 . charAt ( 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 ; } public static void main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; System . out . print ( findMinLength ( s ) ) ; } }