Count of unique palindromic strings of length X from given string | C # implementation to count different palindromic string of length X from the given string S ; 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
using System ; using System . Collections . Generic ; class GFG { static int findways ( string s , int x ) { if ( x > s . Length ) return 0 ; int n = s . Length ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; ++ i ) freq [ s [ i ] - ' a ' ] ++ ; multiset < int > se ; HashSet < int > se = new HashSet < int > ( ) ; for ( int i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . Add ( freq [ i ] ) ; int ans = 1 ; for ( int i = 0 ; i < x / 2 ; ++ i ) { int count = 0 ; foreach ( int u in se ) { if ( u >= 2 ) count ++ ; } if ( count == 0 ) return 0 ; else ans = ans * count ; int [ ] arr = new int [ se . Count ] ; se . CopyTo ( arr ) ; int p = arr [ se . Count - 1 ] ; int val = p ; se . Remove ( p ) ; if ( val > 2 ) se . Add ( val - 2 ) ; } if ( x % 2 != 0 ) { int count = 0 ; foreach ( int u in se ) if ( u > 0 ) count ++ ; ans = ans * count ; } return ans ; } static public void Main ( ) { string s = " aaa " ; int x = 2 ; Console . WriteLine ( findways ( s , x ) ) ; } }