Step 1: Iterate over the entire String
Step 2: Iterate from the end of string
in order to generate different substring
add the subtring to the list
Step 3: Drop kth character from the substring obtained
from above to generate different subsequence.
Step 4: if the subsequence is not in the list then recur.

Below is the implementation of the approach.

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP rogram to print all subsequence of a

// given string.

#include <bits/stdc++.h>

usingnamespacestd;

// set to store all the subsequences

unordered_set<string> st;

// It computes all the subsequence of an string

voidsubsequence(string str)

{

// iterate over the entire string

for(inti = 0; i < str.length(); i++)

{

// iterate from the end of the string

// to generate substrings

for(intj = str.length(); j > i; j--)

{

string sub_str = str.substr(i, j);

st.insert(sub_str);

// drop kth character in the substring

// and if its not in the set then recur

for(intk = 1; k < sub_str.length() - 1; k++)

{

string sb = sub_str;

// drop character from the string

sb.erase(sb.begin() + k);

subsequence(sb);

}

}

}

}

// Driver Code

intmain()

{

string s = "aabc";

subsequence(s);

for(autoi : st)

cout << i << " ";

cout << endl;

return0;

}

// This code is contributed by

// sanjeev2552

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java Program to print all subsequence of a

// given string.

importjava.util.HashSet;

publicclassSubsequence {

// set to store all the subsequences

staticHashSet<String> st = newHashSet<>();

// It computes all the subsequence of an string

staticvoidsubsequence(String str)

{

// iterate over the entire string

for(inti = 0; i < str.length(); i++) {

// iterate from the end of the string

// to generate substrings

for(intj = str.length(); j > i; j--) {

String sub_str = str.substring(i, j);

if(!st.contains(sub_str))

st.add(sub_str);

// drop kth character in the substring

// and if its not in the set then recur

for(intk = 1; k < sub_str.length() - 1; k++) {

StringBuffer sb = newStringBuffer(sub_str);

// drop character from the string

sb.deleteCharAt(k);

if(!st.contains(sb))

;

subsequence(sb.toString());

}

}

}

}

// Driver code

publicstaticvoidmain(String[] args)

{

String s = "aabc";

subsequence(s);

System.out.println(st);

}

}

chevron_right

filter_none

Output:

[aa, a, ab, bc, ac, b, aac, abc, c, aab, aabc]

Method 3 :
One by one fix characters and recursively generates all subsets starting from them. After every recursive call, we remove last character so that next permutation can be generated.

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP program to generate power set in

// lexicographic order.

#include <bits/stdc++.h>

usingnamespacestd;

// str : Stores input string

// n : Length of str.

// curr : Stores current permutation

// index : Index in current permutation, curr

voidprintSubSeqRec(string str, intn,

intindex = -1, string curr = "")

{

// base case

if(index == n)

return;

cout << curr << "\n";

for(inti = index + 1; i < n; i++) {

curr += str[i];

printSubSeqRec(str, n, i, curr);

// backtracking

curr = curr.erase(curr.size() - 1);

}

return;

}

// Generates power set in lexicographic

// order.

voidprintSubSeq(string str)

{

printSubSeqRec(str, str.size());

}

// Driver code

intmain()

{

string str = "cab";

printSubSeq(str);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to generate power set in

// lexicographic order.

classGFG

{

// str : Stores input string

// n : Length of str.

// curr : Stores current permutation

// index : Index in current permutation, curr

staticvoidprintSubSeqRec(String str, intn,

intindex, String curr)

{

// base case

if(index == n)

{

return;

}

System.out.println(curr);

for(inti = index + 1; i < n; i++)

{

curr += str.charAt(i);

printSubSeqRec(str, n, i, curr);

// backtracking

curr = curr.substring(0, curr.length() - 1);

}

}

// Generates power set in

// lexicographic order.

staticvoidprintSubSeq(String str)

{

intindex = -1;

String curr = "";

printSubSeqRec(str, str.length(), index, curr);

}

// Driver code

publicstaticvoidmain(String[] args)

{

String str = "cab";

printSubSeq(str);

}

}

// This code is contributed by PrinciRaj1992

chevron_right

filter_none

Output:

c
ca
cab
cb
a
ab
b

This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details