Check if characters of a given string can be rearranged to form a palindrome | Java implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code
import java . io . * ; import java . math . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean canFormPalindrome ( String str ) { int count [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ ( int ) ( str . charAt ( i ) ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }