Generating distinct subsequences of a given string in lexicographic order | Java 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
import java . util . * ; class GFG { static void generate ( Set < String > st , String s ) { if ( s . length ( ) == 0 ) { return ; } if ( ! st . contains ( s ) ) { st . add ( s ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { String t = s ; t = t . substring ( 0 , i ) + t . substring ( i + 1 ) ; generate ( st , t ) ; } } return ; } public static void main ( String args [ ] ) { String s = " xyz " ; TreeSet < String > st = new TreeSet < > ( ) ; generate ( st , s ) ; for ( String str : st ) { System . out . println ( str ) ; } } }