Count of unique palindromic strings of length X from given string | Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array ; Count the frequency in the string ; Store frequency of the char ; check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; decrease the value of the char we put on the position i and n - i ; different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
function findways ( s , x ) { if ( x > s . length ) return 0 ; var n = s . length ; var freq = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; ++ i ) freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var se = [ ] ; for ( var i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . push ( freq [ i ] ) ; var ans = 1 ; for ( var i = 0 ; i < x / 2 ; ++ i ) { var count = 0 ; se . forEach ( u => { if ( u >= 2 ) count ++ ; } ) ; if ( count == 0 ) return 0 ; else ans = ans * count ; se . sort ( ( a , b ) => a - b ) var val = se [ se . length - 1 ] se . pop ( ) ; if ( val > 2 ) se . push ( val - 2 ) ; } if ( x % 2 != 0 ) { var count = 0 ; se . forEach ( u => { if ( u > 0 ) count ++ ; } ) ; ans = ans * count ; } return ans ; } var s = " " ; var x = 2 ; document . write ( findways ( s , x ) ) ;