Count Distinct Subsequences | Returns count of distinct subsequences of str . ; Iterate from 0 to length of s ; Iterate from 0 to length of s ; Check if i equal to 0 ; Replace levelCount withe allCount + 1 ; If map is less than 0 ; Return answer ; Driver Code
def countSub ( s ) : NEW_LINE INDENT Map = { } NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT Map [ s [ i ] ] = - 1 NEW_LINE DEDENT allCount = 0 NEW_LINE levelCount = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT c = s [ i ] NEW_LINE if ( i == 0 ) : NEW_LINE INDENT allCount = 1 NEW_LINE Map = 1 NEW_LINE levelCount = 1 NEW_LINE continue NEW_LINE DEDENT levelCount = allCount + 1 NEW_LINE if ( Map < 0 ) : NEW_LINE INDENT allCount = allCount + levelCount NEW_LINE DEDENT else : NEW_LINE INDENT allCount = allCount + levelCount - Map NEW_LINE DEDENT Map = levelCount NEW_LINE DEDENT return allCount NEW_LINE DEDENT List = [ " abab " , " gfg " ] NEW_LINE for s in List : NEW_LINE INDENT cnt = countSub ( s ) NEW_LINE withEmptyString = cnt + 1 NEW_LINE print ( " With ▁ empty ▁ string ▁ count ▁ for " , s , " is " , withEmptyString ) NEW_LINE print ( " Without ▁ empty ▁ string ▁ count ▁ for " , s , " is " , cnt ) NEW_LINE DEDENT