Print all subsequences of a string | CPP program to print all subsequence of a given string . ; set to store all the subsequences ; Function computes all the subsequence of an string ; Iterate over the entire string ; Iterate from the end of the string to generate substrings ; Drop kth character in the substring and if its not in the set then recur ; Drop character from the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < string > st ; void subsequence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = str . length ( ) ; j > i ; j -- ) { string sub_str = str . substr ( i , j ) ; st . insert ( sub_str ) ; for ( int k = 1 ; k < sub_str . length ( ) ; k ++ ) { string sb = sub_str ; sb . erase ( sb . begin ( ) + k ) ; subsequence ( sb ) ; } } } } int main ( ) { string s = " aabc " ; subsequence ( s ) ; for ( auto i : st ) cout << i << " ▁ " ; cout << endl ; return 0 ; }