Skip to content
Related Articles

Related Articles

Improve Article

Find the player to last modify a string such that even number of consonants and no vowels are left in the string

  • Last Updated : 28 Apr, 2021

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++




// 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>
Output
Player A

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :