给定一个仅由小写英文字母组成的字符串S ,我们有两个玩家在玩游戏。规则如下:
- 玩家可以从给定的字符串S 中删除任何字符并将其写在纸上空字符串的任何一侧(左侧或右侧)。
- 玩家赢得游戏,如果在任何一步他都可以得到一个长度大于 1 的回文字符串。
- 如果不能形成回文字符串,则宣布玩家 2 获胜。
两者都在玩家 1 开始游戏时发挥最佳。任务是找到游戏的赢家。
例子:
Input: S = “abc”
Output: Player-2
Explanation:
There are all unique characters due to which there
is no way to form a palindromic string of length > 1
Input: S = “abccab”
Output: Player-2
Explanation:
Initially, newString = “” is empty.
Let Player-1 choose character ‘a’ and write it on paper.
Then, S = “bccab” and newString = “a”.
Now Player-2 chooses character ‘a’ from S and writes it on the left side of newString.
Thus, S = “bccb” and newString = “aa”.
Now, newString = “aa” is a palindrome of length 2.
Hence, Player-2 wins.
方法:这个想法是制定一个条件,在这个条件下,玩家 1 总是会成为赢家。如果条件失败,则 Player-2 将赢得比赛。
- 如果给定字符串只有一个唯一字符出现一次,而其余字符出现的次数超过 1,则玩家 1 将成为赢家,否则玩家 2 将始终获胜。
- 如果我们有所有字符在给定的字符串中出现多次,那么玩家 2 总是可以在他的第一回合复制玩家 1 的动作并获胜。
- 此外,如果字符串中的多个字符仅出现一次,则永远无法形成回文字符串(在最佳情况下)。因此,玩家 2 再次获胜。
下面是上述方法的实现:
C++
// C++ Implementation to find
// which player can form a palindromic
// string first in a game
#include
using namespace std;
// Function to find
// winner of the game
int palindromeWinner(string& S)
{
// Array to Maintain frequency
// of the characters in S
int freq[26];
// Initialise freq array with 0
memset(freq, 0, sizeof freq);
// Maintian count of all
// distinct characters
int count = 0;
// Finding frequency of each character
for (int i = 0; i < (int)S.length();
++i) {
if (freq[S[i] - 'a'] == 0)
count++;
freq[S[i] - 'a']++;
}
// Count unique duplicate
// characters
int unique = 0;
int duplicate = 0;
// Loop to count the unique
// duplicate characters
for (int i = 0; i < 26; ++i) {
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
// Condition for Player-1
// to be winner
if (unique == 1 &&
(unique + duplicate) == count)
return 1;
// Else Player-2 is
// always winner
return 2;
}
// Driven Code
int main()
{
string S = "abcbc";
// Function call
cout << "Player-"
<< palindromeWinner(S)
<< endl;
return 0;
}
Java
// Java implementation to find which
// player can form a palindromic
// string first in a game
import java.util.*;
class GFG{
// Function to find
// winner of the game
static int palindromeWinner(String S)
{
// Array to maintain frequency
// of the characters in S
int freq[] = new int[26];
// Initialise freq array with 0
Arrays.fill(freq, 0);
// Maintian count of all
// distinct characters
int count = 0;
// Finding frequency of each character
for(int i = 0; i < (int)S.length(); ++i)
{
if (freq[S.charAt(i) - 'a'] == 0)
count++;
freq[S.charAt(i) - 'a']++;
}
// Count unique duplicate
// characters
int unique = 0;
int duplicate = 0;
// Loop to count the unique
// duplicate characters
for(int i = 0; i < 26; ++i)
{
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
// Condition for Player-1
// to be winner
if (unique == 1 &&
(unique + duplicate) == count)
return 1;
// Else Player-2 is
// always winner
return 2;
}
// Driver Code
public static void main(String s[])
{
String S = "abcbc";
// Function call
System.out.println("Player-" + palindromeWinner(S));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 implementation to find
# which player can form a palindromic
# string first in a game
# Function to find
# winner of the game
def palindromeWinner(S):
# Array to Maintain frequency
# of the characters in S
# initialise freq array with 0
freq = [0 for i in range(0, 26)]
# Maintian count of all
# distinct characters
count = 0
# Finding frequency of each character
for i in range(0, len(S)):
if (freq[ord(S[i]) - 97] == 0):
count += 1
freq[ord(S[i]) - 97] += 1
# Count unique duplicate
# characters
unique = 0
duplicate = 0
# Loop to count the unique
# duplicate characters
for i in range(0, 26):
if (freq[i] == 1):
unique += 1
elif (freq[i] >= 2):
duplicate += 1
# Condition for Player-1
# to be winner
if (unique == 1 and
(unique + duplicate) == count):
return 1
# Else Player-2 is
# always winner
return 2
# Driven Code
S = "abcbc";
# Function call
print("Player-", palindromeWinner(S))
# This code is contributed by Sanjit_Prasad
C#
// C# implementation to find which
// player can form a palindromic
// string first in a game
using System;
class GFG{
// Function to find
// winner of the game
static int palindromeWinner(string S)
{
// Array to maintain frequency
// of the characters in S
int[] freq = new int[26];
// Maintian count of all
// distinct characters
int count = 0;
// Finding frequency of
// each character
for (int i = 0;
i < (int)S.Length; ++i)
{
if (freq[S[i] - 'a'] == 0)
count++;
freq[S[i] - 'a']++;
}
// Count unique duplicate
// characters
int unique = 0;
int duplicate = 0;
// Loop to count the unique
// duplicate characters
for (int i = 0; i < 26; ++i)
{
if (freq[i] == 1)
unique++;
else if (freq[i] >= 2)
duplicate++;
}
// Condition for Player-1
// to be winner
if (unique == 1 &&
(unique + duplicate) ==
count)
return 1;
// Else Player-2 is
// always winner
return 2;
}
// Driver Code
public static void Main(string[] s)
{
string S = "abcbc";
// Function call
Console.Write("Player-" +
palindromeWinner(S));
}
}
// This code is contributed by Chitranayal
Javascript
Player-1
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live