Generating distinct subsequences of a given string in lexicographic order | C ++ program to print all distinct subsequences of a string . ; Finds and stores result in st for a given string s . ; If current string is not already present . ; Traverse current string , one by one remove every character and recur . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generate ( set < string > & st , string s ) { if ( s . size ( ) == 0 ) return ; if ( st . find ( s ) == st . end ( ) ) { st . insert ( s ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { string t = s ; t . erase ( i , 1 ) ; generate ( st , t ) ; } } return ; } int main ( ) { string s = " xyz " ; set < string > st ; set < string > :: iterator it ; generate ( st , s ) ; for ( auto it = st . begin ( ) ; it != st . end ( ) ; it ++ ) cout << * it << endl ; return 0 ; }