给定一个包含小写字母的长度为N的字符串S。两个玩家A和B以最佳方式轮流玩游戏,从玩家 A 开始。 在每次移动中,可以执行以下操作之一:
- 从字符串删除辅音。
- 如果任何字符是元音,则将其转换为任何其他字母。
如果辅音为偶数且字符串没有元音,则玩家输掉游戏。任务是确定游戏的获胜者。如果是平局,则打印D 。
例子:
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
处理方法:解决问题,观察以下情况:
- 如果字符串没有元音和偶数个辅音,则开始游戏的玩家输掉游戏,即玩家B获胜。
- 如果字符串不存在元音和奇数个辅音,则开始游戏的玩家赢得游戏,即玩家 A 获胜。
- 如果给定的字符串中存在单个元音和奇数个辅音,则A可以获胜。
- 如果字符串中存在多个元音,则为平局。
请按照以下步骤解决问题:
- 计算给定字符串S 中的辅音数量并将其存储在一个变量中,比如X 。
- 因此,给定字符串S中元音的数量等于N – X 。
- 如果N – X大于1,则打印D。
- 如果N – X为0且X为偶数,则打印玩家 B。
- 否则打印播放器 A 。
下面是上述方法的实现:
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
Player A
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。