Print all Subsequences of String which Start with Vowel and End with Consonant . | Java Program to generate all the subsequence starting with vowel and ending with consonant . ; Set to store all the subsequences ; It computes all the possible substring that starts with vowel and end with consonent ; iterate over the entire string ; test ith character for vowel ; if the ith character is vowel iterate from end of the string and check for consonant . ; test jth character for consonant . ; once we get a consonant add it to the hashset ; drop each character of the substring and recur to generate all subsequence of the substring ; Utility method to check vowel ; Utility method to check consonant ; Driver code
import java . util . HashSet ; public class Subsequence { static HashSet < String > st = new HashSet < > ( ) ; static void subsequence ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) { for ( int j = ( str . length ( ) - 1 ) ; j >= i ; j -- ) { if ( isConsonant ( str . charAt ( ( j ) ) ) ) { String str_sub = str . substring ( i , j + 1 ) ; st . add ( str_sub ) ; for ( int k = 1 ; k < str_sub . length ( ) - 1 ; k ++ ) { StringBuffer sb = new StringBuffer ( str_sub ) ; sb . deleteCharAt ( k ) ; subsequence ( sb . toString ( ) ) ; } } } } } } static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static boolean isConsonant ( char c ) { return ! isVowel ( c ) ; } public static void main ( String [ ] args ) { String s = " xabcef " ; subsequence ( s ) ; System . out . println ( st ) ; } }