Count Distinct Subsequences | Returns count of distinct subsequences of str . ; Iterate from 0 to s . length ( ) ; Iterate from 0 to s . length ( ) ; Check if i equal to 0 ; Replace levelCount withe allCount + 1 ; If map is less than 0 ; Return answer ; Driver Code
function countSub ( s ) { let map = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { map . set ( s [ i ] , - 1 ) ; } let allCount = 0 ; let levelCount = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { let c = s [ i ] ; if ( i == 0 ) { allCount = 1 ; map . set ( c , 1 ) ; levelCount = 1 ; continue ; } levelCount = allCount + 1 ; if ( map . get ( c ) < 0 ) { allCount = allCount + levelCount ; } else { allCount = allCount + levelCount - map . get ( c ) ; } map . set ( c , levelCount ) ; } return allCount ; } let list = [ " " , " " ] ; for ( let i = 0 ; i < list . length ; i ++ ) { let cnt = countSub ( list [ i ] ) ; let withEmptyString = cnt + 1 ; document . write ( " " + list [ i ] + " " + withEmptyString + " " ) ; document . write ( " " + list [ i ] + " " + cnt + " " ) ; }