Make a palindromic string from given string
Last Updated :
29 Dec, 2022
Given a string S consisting of lower-case English alphabets only, we have two players playing the game. The rules are as follows:
- The player can remove any character from the given string S and write it on paper on any side(left or right) of an empty string.
- The player wins the game, if at any move he can get a palindromic string first of length > 1.
- If a palindromic string cannot be formed, Player-2 is declared the winner.
Both play optimally with player-1 starting the game. The task is to find the winner of the game.
Examples:
Input: S = “abc”
Output: Player-2
Explanation:
There are all unique characters due to which there
is no way to form a palindromic string of length > 1
Input: S = “abccab”
Output: Player-2
Explanation:
Initially, newString = “” is empty.
Let Player-1 choose character ‘a’ and write it on paper.
Then, S = “bccab” and newString = “a”.
Now Player-2 chooses character ‘a’ from S and writes it on the left side of newString.
Thus, S = “bccb” and newString = “aa”.
Now, newString = “aa” is a palindrome of length 2.
Hence, Player-2 wins.
Approach: The idea is to formulate a condition in which Player-1 is always going to be the winner. If the condition fails, then Player-2 will win the game.
- If there is only one unique character occurring once in the given string, and the rest of the characters occurring more than 1, then Player-1 is going to be the winner, else Player-2 will always win.
- If we have all characters that are occurring more than once in the given string, then Player-2 can always copy the Player-1 move in his first turn and wins.
- Also, if we have more than one character in the string occurring one time only, then a palindrome string can never be formed(in the optimal case). Hence, again, Player-2 wins.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int palindromeWinner(string& S)
{
int freq[26];
memset (freq, 0, sizeof freq);
int count = 0;
for ( int i = 0; i < ( int )S.length();
++i) {
if (freq[S[i] - 'a' ] == 0)
count++;
freq[S[i] - 'a' ]++;
}
int unique = 0;
int duplicate = 0;
for ( int i = 0; i < 26; ++i) {
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
if (unique == 1 &&
(unique + duplicate) == count)
return 1;
return 2;
}
int main()
{
string S = "abcbc" ;
cout << "Player-"
<< palindromeWinner(S)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int palindromeWinner(String S)
{
int freq[] = new int [ 26 ];
Arrays.fill(freq, 0 );
int count = 0 ;
for ( int i = 0 ; i < ( int )S.length(); ++i)
{
if (freq[S.charAt(i) - 'a' ] == 0 )
count++;
freq[S.charAt(i) - 'a' ]++;
}
int unique = 0 ;
int duplicate = 0 ;
for ( int i = 0 ; i < 26 ; ++i)
{
if (freq[i] == 1 )
unique++;
else if (freq[i] >= 2 )
duplicate++;
}
if (unique == 1 &&
(unique + duplicate) == count)
return 1 ;
return 2 ;
}
public static void main(String s[])
{
String S = "abcbc" ;
System.out.println( "Player-" + palindromeWinner(S));
}
}
|
Python3
def palindromeWinner(S):
freq = [ 0 for i in range ( 0 , 26 )]
count = 0
for i in range ( 0 , len (S)):
if (freq[ ord (S[i]) - 97 ] = = 0 ):
count + = 1
freq[ ord (S[i]) - 97 ] + = 1
unique = 0
duplicate = 0
for i in range ( 0 , 26 ):
if (freq[i] = = 1 ):
unique + = 1
elif (freq[i] > = 2 ):
duplicate + = 1
if (unique = = 1 and
(unique + duplicate) = = count):
return 1
return 2
S = "abcbc" ;
print ( "Player-" , palindromeWinner(S))
|
C#
using System;
class GFG{
static int palindromeWinner( string S)
{
int [] freq = new int [26];
int count = 0;
for ( int i = 0;
i < ( int )S.Length; ++i)
{
if (freq[S[i] - 'a' ] == 0)
count++;
freq[S[i] - 'a' ]++;
}
int unique = 0;
int duplicate = 0;
for ( int i = 0; i < 26; ++i)
{
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
if (unique == 1 &&
(unique + duplicate) ==
count)
return 1;
return 2;
}
public static void Main( string [] s)
{
string S = "abcbc" ;
Console.Write( "Player-" +
palindromeWinner(S));
}
}
|
Javascript
<script>
function palindromeWinner(S)
{
let freq = new Array(26);
for (let i=0;i<26;i++)
{
freq[i]=0;
}
let count = 0;
for (let i = 0; i < S.length; ++i)
{
if (freq[S[i].charCodeAt(0) - 'a' .charCodeAt(0)] == 0)
count++;
freq[S[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let unique = 0;
let duplicate = 0;
for (let i = 0; i < 26; ++i)
{
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
if (unique == 1 &&
(unique + duplicate) == count)
return 1;
return 2;
}
let S = "abcbc" ;
document.write( "Player-" + palindromeWinner(S));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...