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

• Difficulty Level : Medium
• Last Updated : 21 Jul, 2022

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

 ``

Output

`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.

My Personal Notes arrow_drop_up