Length of the smallest substring which contains all vowels | C # 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
using System ; using System . Collections . Generic ; class GFG { public static int findMinLength ( String s ) { int n = s . Length ; int i = 0 ; Dictionary < char , int > counts = new Dictionary < char , int > ( ) ; List < int > indices = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' o ' s [ i ] == ' i ' s [ i ] == ' u ' ) { if ( counts . ContainsKey ( s [ i ] ) ) { counts [ s [ i ] ] = counts [ s [ i ] ] + 1 ; } else { counts . Add ( s [ i ] , 1 ) ; } indices . Add ( i ) ; } } if ( counts . Count < 5 ) return - 1 ; int flag1 = 0 , flag2 = 0 ; i = 0 ; int j = indices . Count - 1 ; while ( ( j - i ) >= 4 ) { if ( flag1 == 0 && counts [ s [ indices [ i ] ] ] > 1 ) { counts [ s [ indices [ i ] ] ] = counts [ s [ indices [ i ] ] ] - 1 ; i ++ ; } else flag1 = 1 ; if ( flag2 == 0 && counts [ s [ indices [ j ] ] ] > 1 ) { counts [ s [ indices [ j ] ] ] = counts [ s [ indices [ j ] ] ] - 1 ; j -- ; } else flag2 = 1 ; if ( flag1 == 1 && flag2 == 1 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } public static void Main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; Console . Write ( findMinLength ( s ) ) ; } }