Count All Palindromic Subsequence in a given String | PHP program to counts Palindromic Subsequence in a given String using recursion ; Function return the total palindromic subsequence ; Driver code
< ? php $ dp = array_fill ( 0 , 100 , array_fill ( 0 , 1000 , -1 ) ) ; $ str = " abcb " ; $ n = strlen ( $ str ) ; function countPS ( $ i , $ j ) { global $ str , $ dp , $ n ; if ( $ i > $ j ) return 0 ; if ( $ dp [ $ i ] [ $ j ] != -1 ) return $ dp [ $ i ] [ $ j ] ; if ( $ i == $ j ) return $ dp [ $ i ] [ $ j ] = 1 ; else if ( $ str [ $ i ] == $ str [ $ j ] ) return $ dp [ $ i ] [ $ j ] = countPS ( $ i + 1 , $ j ) + countPS ( $ i , $ j - 1 ) + 1 ; else return $ dp [ $ i ] [ $ j ] = countPS ( $ i + 1 , $ j ) + countPS ( $ i , $ j - 1 ) - countPS ( $ i + 1 , $ j - 1 ) ; } echo " Total ▁ palindromic ▁ subsequence ▁ are ▁ : ▁ " . countPS ( 0 , $ n - 1 ) ; ? >