String Subsequence Game | Practice | GeeksforGeeks

Given a string return all unique 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.
NOTE: Return all the unique subsequences in lexicographically sorted order. 

Example 1:
Input: S = "abc"
Output: "ab", "ac", "abc" 
Explanation: "ab", "ac", "abc" are 
the all possible subsequences which 
start with vowel and end with consonant.

Example 2:
Input: S = "aab"
Output: "ab", "aab"
Explanation: "ab", "aab" are the all 
possible subsequences which start 
with vowel and end with consonant.

Your Task:  
You dont need to read input or print anything. Complete the function allPossileSubsequences() which takes S as input parameter and returns all possible subsequences which start with vowel and end with consonant.

Expected Time Complexity: O(n*logn*2n)
Expected Auxiliary Space: O(2n)

Constraints:
1<= |S| <=18
+1
abhiiishek075 days ago
EASY PEASY CPP RECURSIVE :-
    bool isVowel(char ch){
        return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u';
    }
    void solve(string s,int i,string str,set<string>&ans){
        if(i>=s.size()){                      // base constion
            if(str.size()>=2){
                if(isVowel(str[0]) && !isVowel(str[str.size()-1]))
                  ans.insert(str);
            }
            return;
        }
        solve(s,i+1,str+s[i],ans);                   //pick
        solve(s,i+1,str,ans);                   // not pick
    }
    set<string> allPossibleSubsequences(string S) {
        // code here
        set<string>ans;
        string str="";
        solve(S,0,str,ans);
        return ans;
    }
0
sgupta0782 weeks ago
class Solution {
  public:
  
  /*
        b d
        f h
        j n
        p t
        v z
  */
    
    bool checkString(string s){
        int sz=s.size();
        if(s[0]=='a' || s[0]=='e' || s[0]=='i' || s[0]=='o' || s[0]=='u'){
            if((s[sz-1]>='b' && s[sz-1]<='d') || (s[sz-1]>='f' && s[sz-1]<='h') || (s[sz-1]>='j' && s[sz-1]<='n') 
            || (s[sz-1]>='p' && s[sz-1]<='t') || (s[sz-1]>='v' && s[sz-1]<='z')){
                return true;
            } else{
                return false;
            }
            
        } else{
            return false;
        }
    }
    
    void getAll(int index, string S, set<string>& st, string& ans){
        if(index>=S.size()){
            if(checkString(ans)){
                st.insert(ans);
            }
            return;
        }
        
        ans+=S[index];
        getAll(index+1, S, st, ans);
        
        ans=ans.substr(0, ans.size()-1);
        getAll(index+1, S, st, ans);
    }
    
    set<string> allPossibleSubsequences(string S) {
        // code here
        set<string> st;
        string ans="";
        getAll(0, S, st, ans);
        return st;
    }
};
0
premranjan880463 weeks ago
   set<string>st;   void solve(string S, int i, string curr)   {       if(i==S.length())       {           string str=curr;           int n=str.size();           if(str.size()>=2)           {               if((str[0]=='a' || str[0]=='e' || str[0]=='i'|| str[0]=='u'|| str[0]=='o') &&               (str[n-1]!='a'&& str[n-1]!='e'&& str[n-1]!='i'&& str[n-1]!='u'&& str[n-1]!='o'))               st.insert(str);           }           return;       }              solve(S,i+1,curr);       solve(S,i+1,curr+S[i]);   }      set<string> allPossibleSubsequences(string S)    {       solve(S,0,"");       return st;   }
0
prakashish3 weeks ago
Java Most Efficient Solution 
class Solution {
    static TreeSet<String> allPossibleSubsequences(String s) {
        // code here
        TreeSet<String> set = new TreeSet<>();
        ArrayList<String> list = new ArrayList<>();
        getSub(list,s,0,new StringBuilder());
        for(String str : list){
            if(str.length() > 1 && isVowel(str.charAt(0)) && !isVowel(str.charAt(str.length()-1))){
                set.add(str);
            } 
        }
        return set;
    }
    static boolean isVowel(char ch){
        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch =='u'){
            return true;
        }
        return false;
    }
    static void getSub(ArrayList<String> str , String s, int idx, StringBuilder base){
        if(idx == s.length()){
            str.add(base.toString());
            return;
        }
        getSub(str, s,idx+1,base);
        base.append(s.charAt(idx));
        getSub(str, s,idx+1,base);
        base.deleteCharAt(base.length()-1);
    }
}
 
0
ayushsunariya4583 weeks ago
C++ Powerset Solution
  bool isVowel(char& c){
      return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
  }
  void solve(int i, string& s, string &f, set<string>&k) {
    	if (i == s.length()) {
    	    if(isVowel(f[0]) && !isVowel(f[f.length()-1]))
    		    k.insert(f);
    		return;
    	}
    	//picking 
    	f = f + s[i];
    	solve(i + 1, s,  f, k);
    	//poping out while backtracking
    	f.pop_back();
    	solve(i + 1, s,  f, k);
    }
    set<string> allPossibleSubsequences(string s) {
        // code here
        string f="";
        set<string>k;
        solve(0,s,f,k);
        return k;
    }
0
abinashm200674ca2 months ago
char c=op.front();       bool op1 = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');       c=op.back();       bool op2 = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');       if(op1 && !op2)       {           ans.insert(op);       }       if(ip.length()==0)return ; solve(ip.substr(1),op+ip[0],index+1 ); solve(ip.substr(1),op,index+1 );
0
putyavka2 months ago
vector<int> V; // vowels
string T; // current string
void recur(string& S, int idx, set<string>& R) {
    if (T.size() && V[T[0]] && !V[T.back()])
        R.insert(T);
    for (int i = idx; i < S.size(); i++) {
        T.push_back(S[i]);
        recur(S, i + 1, R);
        T.pop_back();
    }
}
set<string> allPossibleSubsequences(string S) {
    V.resize(128);
    V['a'] = V['e'] = V['i'] =
    V['o'] = V['u'] = 1;
    set<string> R;
    recur(S, 0, R);
    return R;
}
0
meghnareddy2 months ago
class Solution {   static TreeSet<String> allPossibleSubsequences(String s) {       // code here       TreeSet<String> res = new TreeSet<>();       List<String> ll = new ArrayList<>();       print_subs(s, "",ll);       List<Character> vowels= Arrays.asList('a','e','i','o','u');              for(String a:ll){           if(a.length()>1 && !a.equals("-1")){               char c=a.charAt(0);               char last=a.charAt(a.length()-1);               if(vowels.contains(c) && !vowels.contains(last)){                   res.add(a);               }           }       }       return res;   }      public static List<String> print_subs(String s, String output, List<String> ll){       if(s.length()==0){            ll.add(output);            return ll;       }             print_subs(s.substring(1), output,ll);       print_subs(s.substring(1), output+s.charAt(0),ll);       return ll;   }   }
+1
aloksinghbais023 months ago
C++ solution having time complexity as O((2^n)*n*log(n)) and space complexity as O((2^n)+n) is as follows :- 
 
Execution Time :- 0.33 / 2.37 sec
 
bool isValid(string output){       if(output.length() <= 1) return (false);       char ch1 = output[0];       char ch2 = output[output.length()-1];       if(!(ch1 == 'a' || ch1 == 'e' || ch1 == 'i' || ch1 == 'o' || ch1 == 'u'))           return (false);              if(ch2 == 'a' || ch2 == 'e' || ch2 == 'i' || ch2 == 'o' || ch2 == 'u')           return (false);              return (true);   }      void helper(string output,int ind,string s,int n,set<string> &ans){       if(ind == n){           if(isValid(output))               ans.insert(output);           return;       }              char ch = s[ind];       helper(output,ind+1,s,n,ans);       helper(output+ch,ind+1,s,n,ans);   }      set<string> allPossibleSubsequences(string S) {       string output;       set<string> ans;       helper(output,0,S,S.length(),ans);       return (ans);   }
+1
balajies4 months ago
There was some problem with python driver code please fix it.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.