Find the winner of the game | Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
def find_winner ( string , n ) : NEW_LINE INDENT string1 = " " ; string2 = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT string1 += string [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT string2 += string [ i ] ; NEW_LINE DEDENT DEDENT string1 = " " . join ( sorted ( string1 ) ) NEW_LINE string2 = " " . join ( sorted ( string2 ) ) NEW_LINE if ( string1 < string2 ) : NEW_LINE INDENT print ( " A " , end = " " ) ; NEW_LINE DEDENT elif ( string2 < string1 ) : NEW_LINE INDENT print ( " B " , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Tie " , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE n = len ( string ) ; NEW_LINE find_winner ( string , n ) ; NEW_LINE DEDENT