Find the player who rearranges the characters to get a palindrome string first | C ++ program to print the winner of the game ; Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner
#include <bits/stdc++.h> NEW_LINE using namespace std ; int returnWinner ( string s , int l ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] & 1 ) cnt ++ ; } if ( cnt == 0 cnt & 1 ) return 1 ; else return 2 ; } int main ( ) { string s = " abaaab " ; int l = s . length ( ) ; int winner = returnWinner ( s , l ) ; cout << " Player - " << winner ; return 0 ; }