📌  相关文章
📜  查找播放器以最后修改字符串,以使字符串中保留偶数个辅音且没有元音

📅  最后修改于: 2021-04-24 20:15:18             🧑  作者: Mango

给定长度为N的包含小写字母的字符串S。从玩家A开始,两个玩家AB依次最佳地玩游戏。在每一步中,都可以执行以下任一操作:

  • 从字符串除去辅音。
  • 如果有任何字符是元音,则将其转换为其他任何字母。

如果辅音数量偶数并且字符串没有元音,则玩家输掉游戏。任务是确定游戏的获胜者。如果平局,则打印D。
例子:

方法:要解决此问题,请观察以下情况:

  • 如果字符串不存在元音和偶数个辅音,则开始游戏的玩家将输掉游戏,即玩家B获胜。
  • 如果字符串没有元音和辅音数量奇数,则开始游戏玩家将赢得游戏,即玩家A获胜。
  • 如果给定的字符串中存在单个元音和奇数个辅音,则演奏者A可以获胜。
  • 如果字符串中存在多个元音,那就是平局。

请按照以下步骤解决问题:

  • 计算给定字符串S中辅音的数量,并将其存储在变量X中
  • 因此,给定字符串S中的元音数量等于N –X
  • 如果N-X大于1,则打印D。
  • 如果N – X0X为偶数,则打印播放器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


输出
Player A




时间复杂度: O(N)
辅助空间: O(N)