给定一个字符串str ,任务是检查它是否可以重新排列以获得特殊的回文字符串。如果我们可以让它打印 YES 否则打印 NO。
字符串称为特殊回文,在回文位置包含相同字符的大写和小写字符。例- ABCCBA是一个特殊的回文,但ABCCBA不是一个特殊的回文。
例子:
Input: str = "ABCcba"
Output: YES
Input: str = "ABCCBA"
Output: NO
处理方法:检查是否一个字符的大写字母的出现是一样一样的字符的小写字母的发生。而且还应该有只有一个奇怪的字符出现。我们通过1增加每个大写字符的频率和1.此之后减小每个小写字符的频率,也应该是零,1或-1的频率阵列中如果别的发生那么我们就直接说NO否则打印是的。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Driver code
int main()
{
string s = "ABCdcba" ;
// creating a array which stores the
// frequency of each character
int u[26] = {0};
int n = s.length() ;
for(int i = 0; i < n ; i++)
{
// Checking if a character is uppercase or not
if(isupper(s[i]))
{
// Increasing by 1 if uppercase
u[s[i] - 65] += 1;
}
else
{
// Decreasing by 1 if lower case
u[s[i] - 97] -= 1 ;
}
}
bool f1 = true ;
// Storing the sum of positive
// numbers in the frequency array
int po = 0 ;
// Storing the sum of negative
// numbers in the frequency array
int ne = 0 ;
for (int i = 0 ; i < 26 ; i++)
{
if (u[i] > 0)
po += u[i] ;
if (u[i] < 0)
ne += u[i] ;
}
// If all character balances out then its Yes
if (po == 0 && ne == 0)
cout << ("YES") << endl;
// If there is only 1 character which
// does not balances then also it is Yes
else if (po == 1 && ne == 0)
cout << ("YES") << endl;
else if (po == 0 && ne == -1)
cout << ("YES") << endl;
else
cout << ("NO") << endl;
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the above approach
public class Improve {
public static void main(String args[])
{
String s = "ABCdcba" ;
// creating a array which stores the
// frequency of each character
int u[] = new int[26];
int n = s.length() ;
for(int i = 0; i < n ; i++)
{
// Checking if a character is uppercase or not
if(Character.isUpperCase(s.charAt(i)))
{
// Increasing by 1 if uppercase
u[s.charAt(i) - 65] += 1 ;
}
else
{
// Decreasing by 1 if lower case
u[s.charAt(i) - 97] -= 1 ;
}
}
boolean f1 = true ;
// Storing the sum of positive
// numbers in the frequency array
int po = 0 ;
// Storing the sum of negative
// numbers in the frequency array
int ne = 0 ;
for (int i = 0 ; i < 26 ; i++)
{
if (u[i] > 0)
po += u[i] ;
if (u[i] < 0)
ne += u[i] ;
}
// If all character balances out then its Yes
if (po == 0 && ne == 0)
System.out.println("YES") ;
// If there is only 1 character which
// does not balances then also it is Yes
else if (po == 1 && ne == 0)
System.out.println("YES") ;
else if (po == 0 && ne == -1)
System.out.println("YES") ;
else
System.out.println("NO") ;
}
// This code is contributed by ANKITRAI1
}
Python3
# Python implementation of the above approach
s = "ABCdcba"
# creating a list which stores the
# frequency of each character
u = [0] * 26
n = len(s)
for i in range(n):
# Checking if a character is uppercase or not
if (s[i].isupper()):
# Increasing by 1 if uppercase
u[ord(s[i]) - 65] += 1
else:
# Decreasing by 1 if lower case
u[ord(s[i]) - 97] -= 1
fl = True
# Storing the sum of positive
# numbers in the frequency array
po = 0
# Storing the sum of negative
# numbers in the frequency array
ne = 0
for i in range(26):
if (u[i] > 0):
po += u[i]
if (u[i] < 0):
ne += u[i]
# If all character balances out then its Yes
if (po == 0 and ne == 0):
print("YES")
# If there is only 1 character which
# does not balances then also it is Yes
elif (po == 1 and ne == 0):
print("YES")
elif (po == 0 and ne == -1):
print("YES")
else:
print("NO")
C#
// C# implementation of the
// above approach
using System;
class GFG
{
public static void Main()
{
string s = "ABCdcba" ;
// creating a array which stores
// the frequency of each character
int[] u = new int[26];
int n = s.Length ;
for(int i = 0; i < n ; i++)
{
// Checking if a character is
// uppercase or not
if(Char.IsUpper(s[i]))
{
// Increasing by 1 if uppercase
u[s[i] - 65] += 1 ;
}
else
{
// Decreasing by 1 if lower case
u[s[i] - 97] -= 1 ;
}
}
// Storing the sum of positive
// numbers in the frequency array
int po = 0 ;
// Storing the sum of negative
// numbers in the frequency array
int ne = 0 ;
for (int i = 0 ; i < 26 ; i++)
{
if (u[i] > 0)
po += u[i] ;
if (u[i] < 0)
ne += u[i] ;
}
// If all character balances
// out then its Yes
if (po == 0 && ne == 0)
Console.Write("YES"+"\n") ;
// If there is only 1 character which
// does not balances then also it is Yes
else if (po == 1 && ne == 0)
Console.Write("YES"+"\n") ;
else if (po == 0 && ne == -1)
Console.Write("YES" + "\n") ;
else
Console.Write("NO" + "\n") ;
}
}
// This code is contributed
// by ChitraNayal
Javascript
输出:
YES
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。