Find the player who rearranges the characters to get a palindrome string first in Python

Suppose we have a string S with lowercase letters, and two players are playing a game. The rules are as follows:

  • A player wins if they can shuffle the characters of the string to form a palindrome string

  • A player cannot win by removing any character from the string

Both players play optimally and Player 1 starts the game. We need to determine the winner.

So, if the input is like "pqpppq", then the output will be Player 1 as Player 1 in the first step arranges the characters to get "ppqqpp" and wins the game.

Algorithm

To solve this problem, we need to understand that a string can form a palindrome if and only if at most one character has an odd frequency. The solution follows these steps:

  • Count the frequency of each character in the string

  • Count how many characters have odd frequencies

  • If the count of odd frequencies is 0 or odd, Player 1 wins

  • Otherwise, Player 2 wins

Example

Let us see the following implementation to get better understanding:

def who_is_the_winner(sequence):
    l = len(sequence)
    freq = [0 for i in range(26)]
    
    # Count frequency of each character
    for i in range(0, l, 1):
        freq[ord(sequence[i]) - ord('a')] += 1
    
    count = 0
    # Count characters with odd frequencies
    for i in range(26):
        if (freq[i] % 2 != 0):
            count += 1
    
    # Player 1 wins if count is 0 or odd
    if (count == 0 or count & 1 == 1):
        return 1
    else:
        return 2

sequence = "pqpppq"
print("Player:", who_is_the_winner(sequence))
Player: 1

How It Works

In the example "pqpppq":

  • Character 'p' appears 4 times (even)

  • Character 'q' appears 2 times (even)

  • Count of odd frequencies = 0

  • Since 0 is even, but our condition checks if count is 0 OR odd, Player 1 wins

Alternative Implementation

Here's a more concise version using Python's Counter:

from collections import Counter

def find_winner(sequence):
    # Count character frequencies
    char_count = Counter(sequence)
    
    # Count characters with odd frequencies
    odd_count = sum(1 for count in char_count.values() if count % 2 == 1)
    
    # Player 1 wins if odd_count is 0 or odd
    return 1 if odd_count == 0 or odd_count % 2 == 1 else 2

# Test the function
test_string = "pqpppq"
winner = find_winner(test_string)
print(f"Winner for '{test_string}': Player {winner}")

# Test with another example
test_string2 = "abcd"
winner2 = find_winner(test_string2)
print(f"Winner for '{test_string2}': Player {winner2}")
Winner for 'pqpppq': Player 1
Winner for 'abcd': Player 2

Conclusion

The key insight is that a palindrome can be formed if at most one character has an odd frequency. Player 1 wins when the count of characters with odd frequencies is either 0 or odd itself.

Updated on: 2026-03-25T09:54:46+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements