Make a palindromic string from given string | Java implementation to find which player can form a palindromic string first in a game ; Function to find winner of the game ; Array to maintain frequency of the characters in S ; Initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call
import java . util . * ; class GFG { static int palindromeWinner ( String S ) { int freq [ ] = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; int count = 0 ; for ( int i = 0 ; i < ( int ) S . length ( ) ; ++ i ) { if ( freq [ S . charAt ( i ) - ' a ' ] == 0 ) count ++ ; freq [ S . charAt ( i ) - ' a ' ] ++ ; } int unique = 0 ; int duplicate = 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } public static void main ( String s [ ] ) { String S = " abcbc " ; System . out . println ( " Player - " + palindromeWinner ( S ) ) ; } }