Find the player who rearranges the characters to get a palindrome string first
Last Updated :
11 Oct, 2023
Given an even length string S consisting of lower-case English alphabets only, we have two players playing the game. The rules are as follows:
- the player wins the game, if, at any move, a player can re-arrange the characters of the string to get a palindrome string.
- if the player cannot win the game, he has to remove any character from the string.
Both players play the game optimally with player-1 starting the game. The task is to print the winner of the game.
Examples:
Input: S = “abaaab”
Output: Player-1
Player-1 in the first step arranges the characters to get “aabbaa” and wins the game.
Input: S = “abca”
Output: Player-2
As the game is being played optimally, player-1 removes ‘a’ to get string “bca” which cannot be rearranged by player-2 in the second move to win the game.
Player-2 optimally removes ‘b’ and the string is now “ca”.
In the third move, player-1 removes “a” as he cannot rearrange the characters, the new string is “c”, which the player-2 at the next move can make a palindrome.
Approach:
- Count the frequencies of each character in a freq[] array.
- Count the characters that occur odd number of times.
- If the count is 0 or an odd number, then Player-1 will always win the game, else player-2 will win because player-2 will make the last move.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int returnWinner(string s, int l)
{
int freq[26];
memset (freq, 0, sizeof freq);
for ( int i = 0; i < l; i++) {
freq[s[i] - 'a' ]++;
}
int cnt = 0;
for ( int i = 0; i < 26; i++) {
if (freq[i] & 1)
cnt++;
}
if (cnt == 0 || cnt & 1)
return 1;
else
return 2;
}
int main()
{
string s = "abaaab" ;
int l = s.length();
int winner = returnWinner(s, l);
cout << "Player-" << winner;
return 0;
}
|
Java
class GfG {
static int returnWinner(String s, int l)
{
int freq[] = new int [ 26 ];
for ( int i = 0 ; i < l; i++) {
freq[s.charAt(i) - 'a' ]++;
}
int cnt = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
if (freq[i] % 2 != 0 )
cnt++;
}
if ((cnt == 0 )|| (cnt & 1 ) == 1 )
return 1 ;
else
return 2 ;
}
public static void main(String[] args)
{
String s = "abaaab" ;
int l = s.length();
int winner = returnWinner(s, l);
System.out.println( "Player-" + winner);
}
}
|
Python3
def returnWinner(s, l):
freq = [ 0 for i in range ( 26 )]
for i in range ( 0 , l, 1 ):
freq[ ord (s[i]) - ord ( 'a' )] + = 1
cnt = 0
for i in range ( 26 ):
if (freq[i] % 2 ! = 0 ):
cnt + = 1
if (cnt = = 0 or cnt & 1 = = 1 ):
return 1
else :
return 2
if __name__ = = '__main__' :
s = "abaaab"
l = len (s)
winner = returnWinner(s, l)
print ( "Player-" , winner)
|
C#
using System;
class GfG
{
static int returnWinner(String s, int l)
{
int []freq = new int [26];
for ( int i = 0; i < l; i++)
{
freq[s[i] - 'a' ]++;
}
int cnt = 0;
for ( int i = 0; i < 26; i++)
{
if (freq[i] % 2 != 0)
cnt++;
}
if ((cnt == 0)|| (cnt & 1) == 1)
return 1;
else
return 2;
}
public static void Main(String[] args)
{
String s = "abaaab" ;
int l = s.Length;
int winner = returnWinner(s, l);
Console.WriteLine( "Player-" + winner);
}
}
|
Javascript
<script>
function returnWinner(s, l)
{
let freq = new Array(26);
freq.fill(0);
for (let i = 0; i < l; i++)
{
freq[s[i].charCodeAt() - 'a' .charCodeAt()]++;
}
let cnt = 0;
for (let i = 0; i < 26; i++)
{
if (freq[i] % 2 != 0)
cnt++;
}
if ((cnt == 0)|| (cnt & 1) == 1)
return 1;
else
return 2;
}
let s = "abaaab" ;
let l = s.length;
let winner = returnWinner(s, l);
document.write( "Player-" + winner);
</script>
|
PHP
<?php
function returnWinner( $s , $l )
{
$freq = array_fill (0, 26, 0);
for ( $i = 0; $i < $l ; $i ++)
{
$freq [ $s [ $i ] - 'a' ]++;
}
$cnt = 0;
for ( $i = 0; $i < 26; $i ++)
{
if ( $freq [ $i ] & 1)
$cnt ++;
}
if ( $cnt == 0 || $cnt & 1)
return 1;
else
return 2;
}
$s = "abaaab" ;
$l = strlen ( $s );
$winner = returnWinner( $s , $l );
echo "Player-" , $winner ;
?>
|
Complexity Analysis:
- Time Complexity: O(l), where l is the length of the string. As, we are using a loop to traverse l times.
- Auxiliary Space: O(26), as we are using extra space for storing the frequencies.
Using a set:
Approach:
In this approach, we will first create a set of all the characters in the string. Then, we will count the number of characters with odd frequency. If this count is greater than 1, Player-2 will win, otherwise, Player-1 will win.
Create a set of all the unique characters in the string s.
For each character c in the set, count the number of occurrences of c in s.
If the count of any character is odd, increment the odd_freq_count variable.
If odd_freq_count is greater than 1, return “Player-2”, indicating that Player-2 will win the game.
Otherwise, return “Player-1”, indicating that Player-1 will win the game.
C++
#include <bits/stdc++.h>
#include <string>
#include <unordered_set>
std::string palindrome_game(std::string s) {
std::unordered_set< char > chars;
for ( char c : s) {
chars.insert(c);
}
int odd_freq_count = 0;
for ( char c : chars) {
if (std::count(s.begin(), s.end(), c) % 2 != 0) {
odd_freq_count++;
}
}
if (odd_freq_count > 1) {
return "Player-2" ;
} else {
return "Player-1" ;
}
}
int main() {
std::string s = "abaaab" ;
std::string result = palindrome_game(s);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.HashSet;
public class Main {
public static String palindromeGame(String s) {
HashSet<Character> chars = new HashSet<>();
for ( char c : s.toCharArray()) {
chars.add(c);
}
int oddFreqCount = 0 ;
for ( char c : chars) {
int charCount = 0 ;
for ( char ch : s.toCharArray()) {
if (ch == c) {
charCount++;
}
}
if (charCount % 2 != 0 ) {
oddFreqCount++;
}
}
if (oddFreqCount > 1 ) {
return "Player-2" ;
} else {
return "Player-1" ;
}
}
public static void main(String[] args) {
String s = "abaaab" ;
String result = palindromeGame(s);
System.out.println(result);
}
}
|
Python3
def palindrome_game(s):
chars = set (s)
odd_freq_count = sum ( 1 for c in chars if s.count(c) % 2 ! = 0 )
if odd_freq_count > 1 :
return "Player-2"
else :
return "Player-1"
s = "abaaab"
result = palindrome_game(s)
print (result)
|
C#
using System;
using System.Collections.Generic;
class Program {
static string PalindromeGame( string s)
{
HashSet< char > chars = new HashSet< char >();
foreach ( char c in s) { chars.Add(c); }
int oddFreqCount = 0;
foreach ( char c in chars)
{
int charFrequency = 0;
foreach ( char originalChar in s)
{
if (originalChar == c) {
charFrequency++;
}
}
if (charFrequency % 2 != 0) {
oddFreqCount++;
}
}
if (oddFreqCount > 1) {
return "Player-2" ;
}
else {
return "Player-1" ;
}
}
static void Main()
{
string s = "abaaab" ;
string result = PalindromeGame(s);
Console.WriteLine(result);
Console.ReadKey();
}
}
|
Javascript
function palindrome_game(s) {
let chars = new Set(s);
let odd_freq_count = 0;
for (let c of chars) {
if ((s.split(c).length - 1) % 2 != 0) {
odd_freq_count++;
}
}
if (odd_freq_count > 1) {
return "Player-2" ;
} else {
return "Player-1" ;
}
}
let s = "abaaab" ;
let result = palindrome_game(s);
console.log(result);
|
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(k), where k is the number of unique characters in the string
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...