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 Intitalise frequency array with 0 ; 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
def findways ( s , x ) : NEW_LINE INDENT if ( x > len ( s ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT n = len ( s ) NEW_LINE freq = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT se = set ( ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT se . add ( freq [ i ] ) NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE for i in range ( x // 2 ) : NEW_LINE INDENT count = 0 NEW_LINE for u in se : NEW_LINE INDENT if ( u >= 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT ans *= count NEW_LINE DEDENT p = list ( se ) NEW_LINE val = p [ - 1 ] NEW_LINE p . pop ( - 1 ) NEW_LINE se = set ( p ) NEW_LINE if ( val > 2 ) : NEW_LINE INDENT se . add ( val - 2 ) NEW_LINE DEDENT DEDENT if ( x % 2 != 0 ) : NEW_LINE INDENT count = 0 NEW_LINE for u in se : NEW_LINE INDENT if ( u > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = ans * count NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaa " NEW_LINE x = 2 NEW_LINE print ( findways ( s , x ) ) NEW_LINE DEDENT