- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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) )
- Related Articles
- 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 player who is the last to remove any character from the beginning of a Binary String
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Python program to get characters from the string using the index
- Program to check string is palindrome with lowercase characters or not in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Python Program to find mirror characters in a string
- Find the first repeated word in a string in Python?
- Python program to find all duplicate characters in a string
- Find the first non-repeating character from a stream of characters in Python
- Find the smallest window in a string containing all characters of another string in Python
- Python program to Find the first non-repeating character from a stream of characters?
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String