Print all Subsequences of String which Start with Vowel and End with Consonant.

Given a string return all possible subsequences which start with vowel and end with consonant. A String is a subsequence of a given String, that is generated by deleting some character of a given string without changing its order.

Input : 'abc'
Output : ab, ac, abc

Input : 'aab'
Output : ab, aab

Question Source: Interview Experience | Set 7

Explanation of the Algorithm:

Step 1: Iterate over the entire String
Step 2: check if the ith character for vowel
Step 3: If true iterate the string from the end,
        if false move to next iteration
Step 4: check the jth character for consonent
        if false move to next iteration 
        if true perform the following 
Step 5: Add the substring starting at index i and
        ending at index j to the hastset.
Step 6: Iterate over the substring drop each character 
        and recur to generate all its subString




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



[ef, ab, ac, aef, abc, abf, af, acf, abcef, abcf, acef, abef]

This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : ManasChhabra2