Count of Distinct Substrings occurring consecutively in a given String | C # program to implement the above approach ; 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 ; Declaring ArrayList to store strings ; 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
using System ; using System . Collections . Generic ; class GFG { static int distinctSimilarSubstrings ( string str ) { int n = str . Length ; if ( n <= 1 ) return 0 ; long [ , ] dp = new long [ n + 1 , n + 1 ] ; List < string > list = new List < string > ( ) ; for ( int j = n - 1 ; j >= 0 ; j -- ) { for ( int 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 ) { list . Add ( str . Substring ( i , j - i ) ) ; } } } return list . Count ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( distinctSimilarSubstrings ( str ) ) ; } }