Minimum removals required such that a string can be rearranged to form a palindrome | Function to find the number of deletions required such that characters of the string can be rearranged to form a palindrome ; Stores frequency of characters ; Store the frequency of each character in frequency array ; Count number of characters with odd frequency ; If count is 1 or 0 , return 0 ; Otherwise , return count - 1 ; Driver Code ; Function call to find minimum number of deletions required
def minDeletions ( str ) : NEW_LINE INDENT fre = [ 0 ] * 26 NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT fre [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( fre [ i ] % 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == 0 or count == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return count - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababbccca " NEW_LINE print ( minDeletions ( str ) ) NEW_LINE DEDENT