- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the player who rearranges the characters to get a palindrome string first in Python
Suppose we have a string S with lowercase letters, now two players are playing the game. The rules are as follows −
The player wins the game, if, at any move, a player can shuffle the characters of the string to get a palindrome string.
The player cannot win when he/she has to remove any character from the string.
We have to keep in mind that both players play the game optimally and player1 starts the game. We have to find the winner of the game.
So, if the input is like "pqpppq", then the output will be Player1 as player-1 in the first step arranges the characters to get “ppqqpp” and wins the game.
To solve this, we will follow these steps −
l := size of sequence
freq := make a list of size 26 and filled with 0
for i in range 0 to l, increase by 1, do
increase freq of sequence[i] by 1
count := 0
for i in range 0 to 25, do
if freq[i] mod 2 is not 0, then
count := count + 1
if count is 0 or count is odd, then
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)] for i in range(0, l, 1): freq[ord(sequence[i]) - ord('a')] += 1 count = 0 for i in range(26): if (freq[i] % 2 != 0): count += 1 if (count == 0 or count & 1 == 1): return 1 else: return 2 sequence = "pqpppq" print("Player:", who_is_the_winner(sequence) )
- How to get first 100 characters of the string in Python?
- How to find the first 10 characters of a string in C#?
- Find the first repeated word in a string in Python?
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Find the first non-repeating character from a stream of characters in Python
- Python program to Find the first non-repeating character from a stream of characters?
- Java Program to Find the Duplicate Characters in a String
- Python Program to find mirror characters in a string
- Program to find out the number of people who get a food packet using Python
- How to extract the first n characters from a string using Java?
- Who is the first Indian Lady to become a doctor?
- Find the first repeated word in a string in Python using Dictionary
- Find the smallest window in a string containing all characters of another string in Python
- Python program to find all duplicate characters in a string