Subsequences of given string consisting of non | Javs program to implement the above approach ; Function to find all the subsequences of the string with non - repeating characters ; Base case ; Insert current subsequence ; If str [ i ] is not present in the current subsequence ; Insert str [ i ] into the set ; Insert str [ i ] into the current subsequence ; Remove str [ i ] from the set ; Not including str [ i ] from the current subsequence ; Utility function to print all subsequences of string with non - repeating characters ; Stores all possible subsequences with non - repeating characters ; Stores subsequence with non - repeating characters ; Traverse all possible subsequences containing non - repeating characters ; Print subsequence ; Driver Code
import java . util . * ; class GFG { public static void FindSub ( HashSet < String > sub , HashSet < Character > ch , String str , String res , int i ) { if ( i == str . length ( ) ) { sub . add ( res ) ; return ; } if ( ! ch . contains ( str . charAt ( i ) ) ) { ch . add ( str . charAt ( i ) ) ; FindSub ( sub , ch , str , res + str . charAt ( i ) , i + 1 ) ; ch . remove ( str . charAt ( i ) ) ; } FindSub ( sub , ch , str , res , i + 1 ) ; } public static void printSubwithUniqueChar ( String str , int N ) { HashSet < String > sub = new HashSet < > ( ) ; HashSet < Character > ch = new HashSet < > ( ) ; FindSub ( sub , ch , str , " " , 0 ) ; for ( String subString : sub ) { System . out . print ( subString + " ▁ " ) ; } } public static void main ( String args [ ] ) { String str = " abac " ; int N = str . length ( ) ; printSubwithUniqueChar ( str , N ) ; } }