Alphabet game

0

0 votes
String Algorithms, Algorithms, Game Theory, Basics of String Manipulation
Problem

You are given the following:

  • Given a string of N lower case alphabets.

Two players A and B play a game turn by turn in which one player can have either of the following moves in his turn:

  1. Remove a consonant from the string.
  2. If it is a vowel, then you may convert it to any other alphabet.

The player unable to make a move loses the game.

Task

Predict the winner of the game if both play optimally and A to start's the game. The game may end in a draw if the game may go on forever.

Note

  • 'a', 'e', 'i', 'o' ,'u' are vowels and others are consonants in lowercase alphabets.

Example

Assumptions

  • N = 2
  • String S = "bc"

Approach

In 1st turn A will remove a consonant either 'b' or 'c'. In the 2nd turn, B will remove the remaining consonant left. In 3rd turn, nothing is left so A loses the game.

Function description

Complete the solve function provided in the editor. This function takes the following 2 parameters and returns the winner of the game if both play optimally:

  • N: Represents the length of string S
  • S: Represents the string of length N

Input: 

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains an integer T denoting the number of test cases. T also denotes the number of times you have to run the solve function on a different set of inputs.
  • For each test case:
    • The first line consists of a single integer denoting the value of N.
    • The second line consists of the string S.

Output

For each test case, print a single character A or B denoting the winner of the game. If there is a draw print D in a new line.
Constraints

1T1031N104

Note: String consists of only lower case alphabets.

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The first line represents the number of test cases, = 3.

For test case 1

  • N = 3
  • String S = "bcd"

Approach

The string is "bcd". Since all are consonants no conversion is possible. Hence in 3 turns the game will definitely finish. In 1st turn A will remove a consonant. In the second turn, B will remove one, while in 3rd again A will remove the last consonant left. In 4th turn, nothing is left so B loses the game.

 

For test case 2

  • N = 4
  • String S = "abcd"

Approach

If A in his first turn converts 'a' to any of the consonants then from the next turn onwards the entire game is reduced to the removal of consonants only. As a whole, there will be 5 turns with A to start and finish hence A wins the game.
 

For test case 3

  • N = 2
  • String S = "ee"

Approach

In this case entire string consists of only vowels. Even if one vowel is removed anyhow still nobody will convert the last vowel to a consonant. Hence most optimally the game ends in a draw. 

Editor Image

?