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:Â
- Count the number of consonants in the given string S and store it in a variable, say X.
- Therefore, the count of vowels in the given string S is equal to N – X.
- Print D if the N – X is greater than 1.
- Print player B if the N – X is 0 and X is even.
- Else print player A.
Below is the implementation of the above approach:
C++
// C++ program for the above approach Â
#include <bits/stdc++.h> using namespace std; Â
// Function to find a winner of the game // if both the player plays optimally void findWinner(string s) {     // Stores the count of vowels     // and consonants     int vowels_count = 0,         consonants_count = 0; Â
    // Traverse the string     for ( int i = 0; i < s.size(); i++) { Â
        // Check if character is vowel         if (s[i] == 'a'             || s[i] == 'e'             || s[i] == 'i'             || s[i] == 'o'             || s[i] == 'u' ) { Â
            // Increment vowels count             vowels_count++;         } Â
        // Otherwise increment the         // consonants count         else {             consonants_count++;         }     } Â
    if (vowels_count == 0) { Â
        // Check if Player B wins         if (consonants_count % 2 == 0) {             cout << "Player B" ;         } Â
        // Check if Player A wins         else {             cout << "Player A" ;         }     } Â
    // Check if Player A wins     else if (vowels_count == 1              && consonants_count % 2 != 0) {         cout << "Player A" ;     } Â
    // If game ends in a Draw     else {         cout << "D" ;     } } Â
// Driver Code int main() {     // Given string s     string s = "abcd" ; Â
    // Function Call     findWinner(s); Â
    return 0; } |
Java
// Java program for the // above approach class GFG{ Â
// Function to find a winner // of the game if both the // player plays optimally static void findWinner( char [] s) {   // Stores the count of vowels   // and consonants   int vowels_count = 0 ,   consonants_count = 0 ; Â
  // Traverse the String   for ( int i = 0 ; i < s.length; i++)   {     // Check if character is vowel     if (s[i] == 'a' ||         s[i] == 'e' ||         s[i] == 'i' ||         s[i] == 'o' ||         s[i] == 'u' )     {       // Increment vowels count       vowels_count++;     } Â
    // Otherwise increment the     // consonants count     else     {       consonants_count++;     }   } Â
  if (vowels_count == 0 )   {     // Check if Player B wins     if (consonants_count % 2 == 0 )     {       System.out.print( "Player B" );     } Â
    // Check if Player A wins     else     {       System.out.print( "Player A" );     }   } Â
  // Check if Player A wins   else if (vowels_count == 1 &&            consonants_count % 2 != 0 )   {     System.out.print( "Player A" );   } Â
  // If game ends in a Draw   else   {     System.out.print( "D" );   } } Â
// Driver Code public static void main(String[] args) {   // Given String s   String s = "abcd" ; Â
  // Function Call   findWinner(s.toCharArray()); } } Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach Â
# Function to find a winner of the game # if both the player plays optimally def findWinner(s):          # Stores the count of     # vowels and consonants     vowels_count = 0     consonants_count = 0 Â
    # Traverse the string     p = len (s)          for i in range ( 0 , p): Â
        # Check if character is vowel         if (s[i] = = 'a' or s[i] = = 'e' or             s[i] = = 'i' or s[i] = = 'o' or             s[i] = = 'u' ):                              # Increment vowels count             vowels_count = vowels_count + 1 Â
        # Otherwise increment the         # consonants count         else :             consonants_count = consonants_count + 1 Â
    if (vowels_count = = 0 ): Â
        # Check if Player B wins         if (consonants_count % 2 = = 0 ):             print ( "Player B" ) Â
        # Check if Player A wins         else :             print ( "Player A" )              # Check if Player A wins     elif (vowels_count = = 1 and        consonants_count % 2 ! = 0 ):         print ( "Player A" ) Â
    # If game ends in a Draw     else :         print ( "D" ) Â
# Driver Code s = "abcd" Â
findWinner(s) Â
# This code is contributed by sallagondaavinashreddy7 |
C#
// C# program for the // above approach using System; class GFG{ Â
// Function to find a winner // of the game if both the // player plays optimally static void findWinner( char [] s) {   // Stores the count of vowels   // and consonants   int vowels_count = 0,   consonants_count = 0; Â
  // Traverse the String   for ( int i = 0; i < s.Length; i++)   {     // Check if character is vowel     if (s[i] == 'a' ||         s[i] == 'e' ||         s[i] == 'i' ||         s[i] == 'o' ||         s[i] == 'u' )     {       // Increment vowels count       vowels_count++;     } Â
    // Otherwise increment the     // consonants count     else     {       consonants_count++;     }   } Â
  if (vowels_count == 0)   {     // Check if Player B wins     if (consonants_count % 2 == 0)     {       Console.Write( "Player B" );     } Â
    // Check if Player A wins     else     {       Console.Write( "Player A" );     }   } Â
  // Check if Player A wins   else if (vowels_count == 1 &&            consonants_count % 2 != 0)   {     Console.Write( "Player A" );   } Â
  // If game ends in a Draw   else   {     Console.Write( "D" );   } } Â
// Driver Code public static void Main(String[] args) {   // Given String s   String s = "abcd" ; Â
  // Function Call   findWinner(s.ToCharArray()); } } Â
// This code is contributed by gauravrajput1 |
Javascript
<script> Â Â Â Â Â Â // JavaScript program for the above approach Â
      // Function to find a winner of the game       // if both the player plays optimally       function findWinner(s)       {                // Stores the count of vowels         // and consonants         var vowels_count = 0,           consonants_count = 0; Â
        // Traverse the string         for ( var i = 0; i < s.length; i++)         {                    // Check if character is vowel           if (             s[i] === "a" ||             s[i] === "e" ||             s[i] === "i" ||             s[i] === "o" ||             s[i] === "u"           )           {                        // Increment vowels count             vowels_count++;           } Â
          // Otherwise increment the           // consonants count           else           {             consonants_count++;           }         } Â
        if (vowels_count === 0)         {                    // Check if Player B wins           if (consonants_count % 2 === 0)           {             document.write( "Player B" );           } Â
          // Check if Player A wins           else           {             document.write( "Player A" );           }         } Â
        // Check if Player A wins         else if (vowels_count === 1 && consonants_count % 2 !== 0) {           document.write( "Player A" );         } Â
        // If game ends in a Draw         else {           document.write( "D" );         }       } Â
      // Driver Code              // Given string s       var s = "abcd" ;              // Function Call       findWinner(s);              // This codeis contributed by rdtank.     </script> |
Player A
Time Complexity: O(N) //only one traversal of the string is reqd.
Auxiliary Space: O (1) // no extra array is used so constant space.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!