Make a palindromic string from given string | 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 ; Driven Code ; Function call
def palindromeWinner ( S ) : NEW_LINE INDENT freq = [ 0 for i in range ( 0 , 26 ) ] NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( S ) ) : NEW_LINE INDENT if ( freq [ ord ( S [ i ] ) - 97 ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT freq [ ord ( S [ i ] ) - 97 ] += 1 NEW_LINE DEDENT unique = 0 NEW_LINE duplicate = 0 NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT if ( freq [ i ] == 1 ) : NEW_LINE INDENT unique += 1 NEW_LINE DEDENT elif ( freq [ i ] >= 2 ) : NEW_LINE INDENT duplicate += 1 NEW_LINE DEDENT DEDENT if ( unique == 1 and ( unique + duplicate ) == count ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT S = " abcbc " ; NEW_LINE print ( " Player - " , palindromeWinner ( S ) ) NEW_LINE