Count Distinct Subsequences | Create an empty set to store the subsequences ; Function for generating the subsequences ; Base Case ; Insert each generated subsequence into the set ; Recursive Case ; When a particular character is taken ; When a particular character isn 't taken ; Driver Code ; Output array for storing the generating subsequences in each call ; Function Call ; Output will be the number of elements in the set
let sn = new Set ( ) ; let m = 0 ; function subsequences ( s , op , i , j ) { if ( i == m ) { op [ j ] = ' \0 ' ; sn . add ( op . join ( " " ) ) ; return ; } else { op [ j ] = s [ i ] ; subsequences ( s , op , i + 1 , j + 1 ) ; subsequences ( s , op , i + 1 , j ) ; return ; } } let str = " " ; m = str . length ; let n = Math . pow ( 2 , m ) + 1 ; let op = new Array ( n ) ; subsequences ( str , op , 0 , 0 ) ; document . write ( sn . size ) ;