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
def get_index ( ch ) : NEW_LINE INDENT if ( ch == ' a ' ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( ch == ' e ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( ch == ' i ' ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif ( ch == ' o ' ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT elif ( ch == ' u ' ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT def findMinLength ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = n + 1 NEW_LINE start = 0 NEW_LINE count = [ 0 ] * 5 NEW_LINE for x in range ( n ) : NEW_LINE INDENT idx = get_index ( s [ x ] ) NEW_LINE if ( idx != - 1 ) : NEW_LINE INDENT count [ idx ] += 1 NEW_LINE DEDENT idx_start = get_index ( s [ start ] ) NEW_LINE while ( idx_start == - 1 or count [ idx_start ] > 1 ) : NEW_LINE INDENT if ( idx_start != - 1 ) : NEW_LINE INDENT count [ idx_start ] -= 1 NEW_LINE DEDENT start += 1 NEW_LINE if ( start < n ) : NEW_LINE INDENT idx_start = get_index ( s [ start ] ) NEW_LINE DEDENT DEDENT if ( count [ 0 ] > 0 and count [ 1 ] > 0 and count [ 2 ] > 0 and count [ 3 ] > 0 and count [ 4 ] > 0 ) : NEW_LINE INDENT ans = min ( ans , x - start + 1 ) NEW_LINE DEDENT DEDENT if ( ans == n + 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aaeebbeaccaaoiuooooooooiuu " NEW_LINE print ( findMinLength ( s ) ) NEW_LINE DEDENT