Longest Ordered Subsequence of Vowels | Python3 program to find the longest subsequence of vowels in the specified order ; Mapping values for vowels ; Function to check if given subsequence contains all the vowels or not ; not contain vowel ; Function to find the longest subsequence of vowels in the given string in specified order ; If we have reached the end of the string , return the subsequence if it is valid , else return an empty list ; If there is no vowel in the subsequence yet , add vowel at current index if it is ' a ' , else move on to the next character in the string ; If the last vowel in the subsequence until now is same as the vowel at current index , add it to the subsequence ; If the vowel at the current index comes right after the last vowel in the subsequence , we have two options : either to add the vowel in the subsequence , or move on to next character . We choose the one which gives the longest subsequence . ; Driver Code
vowels = [ ' a ' , ' e ' , ' i ' , ' o ' , ' u ' ] NEW_LINE mapping = { ' a ' : 0 , ' e ' : 1 , ' i ' : 2 , ' o ' : 3 , ' u ' : 4 } NEW_LINE def isValidSequence ( subList ) : NEW_LINE INDENT for vowel in vowels : NEW_LINE INDENT if vowel not in subList : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def longestSubsequence ( string , subList , index ) : NEW_LINE INDENT if index == len ( string ) : NEW_LINE INDENT if isValidSequence ( subList ) == True : NEW_LINE INDENT return subList NEW_LINE DEDENT else : NEW_LINE INDENT return [ ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if len ( subList ) == 0 : NEW_LINE INDENT if string [ index ] != ' a ' : NEW_LINE INDENT return longestSubsequence ( string , subList , index + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return longestSubsequence ( string , subList +   \ [ string [ index ] ] , index + 1 ) NEW_LINE DEDENT DEDENT elif mapping [ subList [ - 1 ] ] == mapping [ string [ index ] ] : NEW_LINE INDENT return longestSubsequence ( string , subList +   \ [ string [ index ] ] , index + 1 ) NEW_LINE DEDENT elif ( mapping [ subList [ - 1 ] ] + 1 ) == mapping [ string [ index ] ] : NEW_LINE INDENT sub1 = longestSubsequence ( string , subList +   \ [ string [ index ] ] , index + 1 ) NEW_LINE sub2 = longestSubsequence ( string , subList , index + 1 ) NEW_LINE if len ( sub1 ) > len ( sub2 ) : NEW_LINE INDENT return sub1 NEW_LINE DEDENT else : NEW_LINE INDENT return sub2 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return longestSubsequence ( string , subList , index + 1 ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " aeiaaioooauuaeiou " NEW_LINE subsequence = longestSubsequence ( string , [ ] , 0 ) NEW_LINE if len ( subsequence ) == 0 : NEW_LINE INDENT print ( " No ▁ subsequence ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( subsequence ) NEW_LINE DEDENT DEDENT