📌  相关文章
📜  找到重新排列字符以获得回文字符串的玩家

📅  最后修改于: 2022-05-13 01:57:07.199000             🧑  作者: Mango

找到重新排列字符以获得回文字符串的玩家

给定一个仅由小写英文字母组成的偶数长度字符串S ,我们有两个玩家在玩这个游戏。规则如下:

  • 玩家赢得游戏,如果在任何移动中,玩家可以重新排列字符的字符串以获得回文字符串。
  • 如果玩家不能赢得比赛,他必须从字符串中删除任何字符

两名玩家都以最佳方式玩游戏,玩家 1 开始游戏。任务是打印游戏的获胜者。
例子:

方法:

  • 计算 freq[] 数组中每个字符的频率。
  • 计算出现奇数次的字符。
  • 如果计数为0奇数,则玩家 1将始终赢得游戏,否则玩家 2 将获胜,因为玩家 2 将走最后一步。

下面是上述方法的实现:

C++
// C++ program to print the winner of the game
#include 
using namespace std;
 
// Function that returns the winner of the game
int returnWinner(string s, int l)
{
    // Initialize the freq array to 0
    int freq[26];
    memset(freq, 0, sizeof freq);
 
    // Iterate and count the frequencies
    // of each character in the string
    for (int i = 0; i < l; i++) {
        freq[s[i] - 'a']++;
    }
 
    int cnt = 0;
 
    // Count the odd occurring character
    for (int i = 0; i < 26; i++) {
 
        // If odd occurrence
        if (freq[i] & 1)
            cnt++;
    }
 
    // Check condition for Player-1 winning the game
    if (cnt == 0 || cnt & 1)
        return 1;
    else
        return 2;
}
 
// Driver code
int main()
{
    string s = "abaaab";
    int l = s.length();
 
    // Function call that returns the winner
    int winner = returnWinner(s, l);
 
    cout << "Player-" << winner;
    return 0;
}


Java
// Java program to print the winner of the game
class GfG {
// Function that returns the winner of the game
static int returnWinner(String s, int l)
{
    // Initialize the freq array to 0
    int freq[]  =new int[26];
 
    // Iterate and count the frequencies
    // of each character in the string
    for (int i = 0; i < l; i++) {
        freq[s.charAt(i) - 'a']++;
    }
 
    int cnt = 0;
 
    // Count the odd occurring character
    for (int i = 0; i < 26; i++) {
 
        // If odd occurrence
        if (freq[i] % 2 != 0)
            cnt++;
    }
 
    // Check condition for Player-1 winning the game
    if ((cnt == 0)|| (cnt & 1) == 1)
        return 1;
    else
        return 2;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "abaaab";
    int l = s.length();
 
    // Function call that returns the winner
    int winner = returnWinner(s, l);
 
    System.out.println("Player-" + winner);
}
}


Python3
# Python 3 program to print the winner of the game
 
# Function that returns the winner of the game
def returnWinner(s, l):
     
    # Initialize the freq array to 0
    freq = [0 for i in range(26)]
 
    # Iterate and count the frequencies
    # of each character in the string
    for i in range(0, l, 1):
        freq[ord(s[i]) - ord('a')] += 1
 
    cnt = 0
 
    # Count the odd occurring character
    for i in range(26):
         
        # If odd occurrence
        if (freq[i] % 2 != 0):
            cnt += 1
 
    # Check condition for Player-1
    # winning the game
    if (cnt == 0 or cnt & 1 == 1):
        return 1
    else:
        return 2
 
# Driver code
if __name__ == '__main__':
    s = "abaaab"
    l = len(s)
 
    # Function call that returns the winner
    winner = returnWinner(s, l)
 
    print("Player-", winner)
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to print the winner of the game
using System;
 
class GfG
{
     
// Function that returns the winner of the game
static int returnWinner(String s, int l)
{
    // Initialize the freq array to 0
    int []freq = new int[26];
 
    // Iterate and count the frequencies
    // of each character in the string
    for (int i = 0; i < l; i++)
    {
        freq[s[i] - 'a']++;
    }
 
    int cnt = 0;
 
    // Count the odd occurring character
    for (int i = 0; i < 26; i++)
    {
 
        // If odd occurrence
        if (freq[i] % 2 != 0)
            cnt++;
    }
 
    // Check condition for Player-1 winning the game
    if ((cnt == 0)|| (cnt & 1) == 1)
        return 1;
    else
        return 2;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "abaaab";
    int l = s.Length;
 
    // Function call that returns the winner
    int winner = returnWinner(s, l);
 
    Console.WriteLine("Player-" + winner);
}
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
Player-1