Determine the winner of a game of deleting Characters from a String | Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
def determineWinner ( str ) : NEW_LINE INDENT A = [ 0 for i in range ( 9 ) ] ; NEW_LINE sum1 = 0 ; sum2 = 0 ; NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT A [ int ( str [ i ] ) ] += 1 ; NEW_LINE DEDENT for i in range ( 0 , 9 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT sum1 = sum1 + A [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT sum2 = sum2 + A [ i ] ; NEW_LINE DEDENT DEDENT if ( sum1 == sum2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT elif ( sum1 > sum2 ) : NEW_LINE INDENT print ( " Player ▁ 1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Player ▁ 2" ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "78787" ; NEW_LINE determineWinner ( str ) ; NEW_LINE DEDENT