Given a string S of length N containing lowercase alphabets. Two players A and B play a game optimally in turns, starting with player A. In each move, either of the following operations can be performed:
- Remove a consonant from the string.
- If any character is a vowel, then convert it into any other alphabet.
A player loses the game if there is an even number of consonants and no vowels left in the string. The task is to determine the winner of the game. In case of a draw, print D.
Examples:
Input: S = "abcd"
Output: Player A
Explanation:
Player A can win by performing the following moves:
Move 1: A changes a to f. Therefore, S = "fbcd"
Move 2: B removes f. Therefore, S = "bcd".
Move 3: A removes b. Therefore, S = "cd".
Move 4: B removes c. Therefore, S = "d".
Move 5: A removes d. Therefore, S = "".
Now in B's turn, S have no vowels and an even number of consonants i.e., 0.
Input: S = "abcde"
Output: D
Approach: To solve the problem, observe the following cases:
- If no vowels and an even number of consonants are present in the string then the player who starts the game loses the game, i.e. player B wins.
- If no vowels and an odd number of consonants are present in the string then the player who starts the game wins the game, i.e. player A win.
- If a single vowel and an odd number of consonants are present in the given string player A can win.
- If more than one vowels are present in the string, it’s a draw.
Follow the below steps to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findWinner(string s)
{
int vowels_count = 0,
consonants_count = 0;
for ( int i = 0; i < s.size(); i++) {
if (s[i] == 'a'
|| s[i] == 'e'
|| s[i] == 'i'
|| s[i] == 'o'
|| s[i] == 'u' ) {
vowels_count++;
}
else {
consonants_count++;
}
}
if (vowels_count == 0) {
if (consonants_count % 2 == 0) {
cout << "Player B" ;
}
else {
cout << "Player A" ;
}
}
else if (vowels_count == 1
&& consonants_count % 2 != 0) {
cout << "Player A" ;
}
else {
cout << "D" ;
}
}
int main()
{
string s = "abcd" ;
findWinner(s);
return 0;
}
|
Java
class GFG{
static void findWinner( char [] s)
{
int vowels_count = 0 ,
consonants_count = 0 ;
for ( int i = 0 ; i < s.length; i++)
{
if (s[i] == 'a' ||
s[i] == 'e' ||
s[i] == 'i' ||
s[i] == 'o' ||
s[i] == 'u' )
{
vowels_count++;
}
else
{
consonants_count++;
}
}
if (vowels_count == 0 )
{
if (consonants_count % 2 == 0 )
{
System.out.print( "Player B" );
}
else
{
System.out.print( "Player A" );
}
}
else if (vowels_count == 1 &&
consonants_count % 2 != 0 )
{
System.out.print( "Player A" );
}
else
{
System.out.print( "D" );
}
}
public static void main(String[] args)
{
String s = "abcd" ;
findWinner(s.toCharArray());
}
}
|
Python3
def findWinner(s):
vowels_count = 0
consonants_count = 0
p = len (s)
for i in range ( 0 , p):
if (s[i] = = 'a' or s[i] = = 'e' or
s[i] = = 'i' or s[i] = = 'o' or
s[i] = = 'u' ):
vowels_count = vowels_count + 1
else :
consonants_count = consonants_count + 1
if (vowels_count = = 0 ):
if (consonants_count % 2 = = 0 ):
print ( "Player B" )
else :
print ( "Player A" )
elif (vowels_count = = 1 and
consonants_count % 2 ! = 0 ):
print ( "Player A" )
else :
print ( "D" )
s = "abcd"
findWinner(s)
|
C#
using System;
class GFG{
static void findWinner( char [] s)
{
int vowels_count = 0,
consonants_count = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == 'a' ||
s[i] == 'e' ||
s[i] == 'i' ||
s[i] == 'o' ||
s[i] == 'u' )
{
vowels_count++;
}
else
{
consonants_count++;
}
}
if (vowels_count == 0)
{
if (consonants_count % 2 == 0)
{
Console.Write( "Player B" );
}
else
{
Console.Write( "Player A" );
}
}
else if (vowels_count == 1 &&
consonants_count % 2 != 0)
{
Console.Write( "Player A" );
}
else
{
Console.Write( "D" );
}
}
public static void Main(String[] args)
{
String s = "abcd" ;
findWinner(s.ToCharArray());
}
}
|
Javascript
<script>
function findWinner(s)
{
var vowels_count = 0,
consonants_count = 0;
for ( var i = 0; i < s.length; i++)
{
if (
s[i] === "a" ||
s[i] === "e" ||
s[i] === "i" ||
s[i] === "o" ||
s[i] === "u"
)
{
vowels_count++;
}
else
{
consonants_count++;
}
}
if (vowels_count === 0)
{
if (consonants_count % 2 === 0)
{
document.write( "Player B" );
}
else
{
document.write( "Player A" );
}
}
else if (vowels_count === 1 && consonants_count % 2 !== 0) {
document.write( "Player A" );
}
else {
document.write( "D" );
}
}
var s = "abcd" ;
findWinner(s);
</script>
|
Time Complexity: O(N) //only one traversal of the string is reqd.
Auxiliary Space: O (1) // no extra array is used so constant space.