Wednesday, 6 September 2017

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

import java.util.HashSet;

public class Example {

   
    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 !(c == 'a' || c == 'e' || c == 'i' || c == 'o'
                                              || c == 'u');
    }

    public static void main(String[] args)
    {
        String s = "abcdefg";
        subsequence(s);
        System.out.println(st);
    }
}

No comments:

Post a Comment

test your brain

https://www.youtube.com/watch?v=bEU9dkOG7co&feature=youtu.be