Distinct palindromic sub | Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i ] [ i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 ] [ end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start ] [ end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver Code
function palindromeSubStrs ( s ) { let dp = new Array ( s . length ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < dp . length ; i ++ ) { for ( let j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let st , end , i , len ; let m = new Map ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { dp [ i ] [ i ] = 1 ; m . set ( s . substr ( i , i + 1 ) , true ) ; } for ( i = 0 ; i < s . length - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { dp [ i ] [ i + 1 ] = 1 ; m . set ( s . substr ( i , i + 2 ) , true ) ; } else dp [ i ] [ i + 1 ] = 0 ; } for ( len = 3 ; len <= s . length ; len ++ ) { for ( st = 0 ; st <= s . length - len ; st ++ ) { end = st + len - 1 ; if ( s [ st ] == s [ end ] && dp [ st + 1 ] [ end - 1 ] == 1 ) { dp [ st ] [ end ] = 1 ; m . set ( s . substr ( st , end + 1 ) , true ) ; } else dp [ st ] [ end ] = 0 ; } } return m . size ; } let s = " " ; document . write ( palindromeSubStrs ( s ) ) ;