检查字符串是否可以重新排列以形成特殊回文

本文概述

  • C ++
  • Java
  • Python3
  • C#
给定一个字符串str,任务是检查它是否可以重新排列以得到一个特殊的回文字符串。如果我们能让它打印YES,否则打印NO。
一个字符串被称为特殊回文,它包含同一个字符在回文位置上的一个大写字母和一个小写字母。例如:ABCcba是一个特殊的回文,但ABCcba不是一个特殊的回文。
例子:
Input: str = "ABCcba" Output: YESInput: str = "ABCCBA" Output: NO

方法:检查一个字符的大写字母是否与该字符的小写字母相同。而且应该只出现一个奇特的字符。我们将每个大写字符的频率增加1, 并将每个小写字符的频率减少1。此后, 频率数组中应该为零, 1或-1。如果发生其他情况, 则直接说不, 否则打印是。
下面是上述方法的实现:
C ++
//C++ implementation of the above approach #include< bits/stdc++.h> 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展开 - 65] += 1; } else { //Decreasing by 1 if lower case u展开 - 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展开 += 1 ; } else { //Decreasing by 1 if lower case u展开 -= 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展开 - 65] += 1 ; } else { //Decreasing by 1 if lower case u展开 - 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

【检查字符串是否可以重新排列以形成特殊回文】输出如下:
YES

    推荐阅读