Count of Distinct Substrings occurring consecutively in a given String | Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Stores the distinct substring ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver Code
function distinctSimilarSubstrings ( str ) { var n = str . length ; if ( n <= 1 ) { return 0 ; } var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; var substrings = new Set ( ) ; for ( var j = n - 1 ; j >= 0 ; j -- ) { for ( var i = j - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == str [ j ] ) { dp [ i ] [ j ] = dp [ i + 1 ] [ j + 1 ] + 1 ; } else { dp [ i ] [ j ] = 0 ; } if ( dp [ i ] [ j ] >= j - i ) { substrings . add ( str . substring ( i , j ) ) ; } } } return substrings . size ; } var str = " " ; document . write ( distinctSimilarSubstrings ( str ) ) ;