Given a string return all possible subsequences which start with a vowel and end with a 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.
Examples:
Input : 'abc'
Output : ab, ac, abc
Input : 'aab'
Output : ab, aab
Question Source: Yatra.com Interview Experience | Set 7
BRUTE METHOD USING BIT MANIPULATION:
Intuition:
- Declare a TreeSet<String> to store all the subsequences of string s.
- we pass the string s to a function where the subsequences are calculated.
- Then we check if first, the character of the string is a vowel and the last character is consonant or not.
- If so, then we add it to the ans list.
- At last, return the list.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
set<string> st;
void findSubsequences(string s) {
int n = s.length();
for ( int num = 0; num < (1 << n); num++) {
string sub = "" ;
for ( int i = 0; i < n; i++) {
if ((num & (1 << i)) != 0) {
sub += s[i];
}
}
if (sub.length() > 0) {
st.insert(sub);
}
}
}
bool isVowel( char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u' );
}
bool isConsonant( char c) {
return !(c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
set<string> allPossibleSubsequences(string s) {
findSubsequences(s);
set<string> ans;
for (string i : st) {
if (isVowel(i[0]) && isConsonant(i[i.length() - 1])) {
ans.insert(i);
}
}
st.clear();
return ans;
}
int main() {
string s = "xabcef" ;
set<string> result = allPossibleSubsequences(s);
for (string str : result) {
cout << str << " " ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static TreeSet<String> st = new TreeSet<String>();
static void findsubsequences(String s)
{
int n = s.length();
for ( int num = 0 ; num < ( 1 << n); num++) {
String sub = "" ;
for ( int i = 0 ; i < n; i++) {
if ((num & ( 1 << i)) != 0 ) {
sub += s.charAt(i);
}
}
if (sub.length() > 0 ) {
st.add(sub);
}
}
}
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' );
}
static TreeSet<String> allPossibleSubsequences(String s)
{
findsubsequences(s);
TreeSet<String> ans = new TreeSet<String>();
for (String i : st) {
if (isVowel(i.charAt( 0 ))
&& isConsonant(i.charAt(i.length() - 1 )))
ans.add(i);
}
st.clear();
return ans;
}
public static void main(String[] args)
{
String s = "xabcef" ;
System.out.println(allPossibleSubsequences(s));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet< string > st = new SortedSet< string >();
static void findsubsequences( string s)
{
int n = s.Length;
for ( int num = 0; num < (1 << n); num++)
{
string sub = "" ;
for ( int i = 0; i < n; i++)
{
if ((num & (1 << i)) != 0)
{
sub += s[i];
}
}
if (sub.Length > 0)
{
st.Add(sub);
}
}
}
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' );
}
static bool isConsonant( char c)
{
return !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' );
}
static SortedSet< string > allPossibleSubsequences( string s)
{
findsubsequences(s);
SortedSet< string > ans = new SortedSet< string >();
foreach ( string i in st)
{
if (isVowel(i[0]) && isConsonant(i[i.Length - 1]))
ans.Add(i);
}
st.Clear();
return ans;
}
public static void Main( string [] args)
{
string s = "xabcef" ;
SortedSet< string > result = allPossibleSubsequences(s);
foreach ( string str in result)
{
Console.Write(str+ " " );
}
}
}
|
Output
[ab, abc, abcef, abcf, abef, abf, ac, acef, acf, aef, af, ef]
Time Complexity: O(n* logn* (2^n))
Space Complexity: O(N) since we are using Tree Set.
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 consonant
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 hashset.
- Step 6: Iterate over the substring drop each character and recur to generate all its subString
C++
#include <bits/stdc++.h>
using namespace std;
set<string> st;
bool isVowel( char c)
{
return (c == 'a' or c == 'e' or
c == 'i' or c == 'o' or
c == 'u' );
}
bool isConsonant( char c)
{
return !isVowel(c);
}
void subsequence(string str)
{
for ( int i = 0; i < str.length(); i++)
{
if (isVowel(str[i]))
{
for ( int j = str.length() - 1; j >= i; j--)
{
if (isConsonant(str[j]))
{
string str_sub = str.substr(i, j + 1);
st.insert(str_sub);
for ( int k = 1; k < str_sub.length() - 1; k++)
{
string sb = str_sub;
sb.erase(sb.begin() + k);
subsequence(sb);
}
}
}
}
}
}
int main()
{
string s = "xabcef" ;
subsequence(s);
for ( auto i : st)
cout << i << " " ;
cout << endl;
return 0;
}
|
Java
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);
}
}
|
Python3
st = set ()
def isVowel(c):
return (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' )
def isConsonant(c):
return not isVowel(c)
def subsequence( Str ):
global st
for i in range ( len ( Str )):
if (isVowel( Str [i])):
for j in range ( len ( Str ) - 1 ,i - 1 , - 1 ):
if (isConsonant( Str [j])):
str_sub = Str [i : i + j + 1 ]
st.add(str_sub)
for k in range ( 1 , len (str_sub)):
sb = str_sub
sb = sb.replace(sb[k],"")
subsequence(sb)
s = "xabcef"
subsequence(s)
for i in st:
print (i,end = " " )
print ()
|
C#
using System;
using System.Collections.Generic;
using System.Text;
class Subsequence
{
static HashSet<String> st = new HashSet<String>();
static void subsequence(String str)
{
for ( int i = 0; i < str.Length; i++)
{
if (isVowel(str[i]))
{
for ( int j = (str.Length - 1); j >= i; j--)
{
if (isConsonant(str[j]))
{
String str_sub = str.Substring(i, j -i + 1);
st.Add(str_sub);
for ( int k = 1; k < str_sub.Length - 1; k++)
{
StringBuilder sb = new StringBuilder(str_sub);
sb.Remove(k, 1);
subsequence(sb.ToString());
}
}
}
}
}
}
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u' );
}
static bool isConsonant( char c)
{
return !isVowel(c);
}
public static void Main(String[] args)
{
String s = "xabcef" ;
subsequence(s);
foreach (String str in st)
Console.Write(str + ", " );
}
}
|
Javascript
<script>
let st = new Set();
function isVowel(c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
function isConsonant(c)
{
return !isVowel(c);
}
function subsequence(str)
{
for (let i = 0; i < str.length; i++)
{
if (isVowel(str[i]))
{
for (let j = str.length - 1; j >= i; j--)
{
if (isConsonant(str[j]))
{
let str_sub = str.substring(i, i + j + 1);
st.add(str_sub);
for (let k = 1; k < str_sub.length - 1; k++)
{
let sb = str_sub;
sb = sb.replace(sb[k], "" );
subsequence(sb);
}
}
}
}
}
}
let s = "xabcef" ;
subsequence(s);
for (let i of st)
document.write(i, " " );
document.write( "</br>" );
</script>
|
Output
ab abc abce abcef abcf abef abf ac acef acf aef af ef
Time complexity: O(2^n) where n is the length of the string
Auxiliary space: O(2^n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!