Minimum removals required such that a string can be rearranged to form a palindrome | Java program for the above approach ; 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
public class GFG { static int minDeletions ( String str ) { int fre [ ] = new int [ 26 ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { fre [ str . charAt ( i ) - ' a ' ] += 1 ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % 2 == 1 ) { count += 1 ; } } if ( count == 0 count == 1 ) { return 0 ; } else { return count - 1 ; } } public static void main ( String [ ] args ) { String str = " ababbccca " ; System . out . println ( minDeletions ( str ) ) ; } }