Check if a string can be rearranged to form special palindrome
Last Updated :
09 Mar, 2023
Given a string str, the task is to check if it can be rearranged to get a special palindromic string. If we can make it print YES else print NO.
A string is called special Palindrome that contains an uppercase and a lower case character of the same character in the palindrome position.
Example: ABCcba is a special palindrome but ABCCBA is not a special palindrome.
Examples:
Input: str = “ABCcba”
Output: YES
Input: str = “ABCCBA”
Output: NO
Approach: Check if the occurrence of the uppercase letter of a character is same as the occurrence of lower case letter of the same character. And also there should be only one odd occurring character. We increase the frequency of each uppercase character by 1 and decrease the frequency of each lowercase character by 1. After this, there should be either zero, 1 or -1 in the frequency array If anything else occurs then we directly say NO else print YES.
Algorithm:
Step 1: Initialize “s” as “ABCdcba” in the input string.
Step 2: Create an integer array “u” with a size of 26 and initialize it with all values set to zero to hold the frequency of each character. Step 3: With a for loop, cycle through each character of the string “n” until it reaches the desired length.
Step 4: Use the Character.isUpperCase() function to determine whether each character in the string is uppercase or lowercase.
Step 5: Using the ASCII value of the character to retrieve the associated index, increase the frequency in the “u” array by 1 if the character is uppercase.
Step 6: Use the character’s ASCII value to access the associated index and reduce the frequency in the “u” array by 1 if the character is lowercase.
Step 7: Boolean variable “f1” should be set to true. Step 8: Set the values of two integer variables, “po” and “ne,” to 0 so that they will, respectively, store the sums of positive and negative numbers in the “u” array.
Step 9: Use a for loop to iterate through the “u” array.
Step 10: Add a positive value to the “po” variable if the value at an index in the “u” array is positive.
Step 11: Add the value at that index, if it’s negative, to the “ne” variable.
Step 12: Print “YES” if the product of the positive and negative values is zero.
Step 13: Print “YES” if the product of all positive integers is 1 and the product of all negative numbers is 0.
Step 14: Print “YES” if the total of the positive and negative values is 0, respectively.
Step 15: Print “NO” if not.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s = "ABCdcba" ;
int u[26] = {0};
int n = s.length() ;
for ( int i = 0; i < n ; i++)
{
if ( isupper (s[i]))
{
u[s[i] - 65] += 1;
}
else
{
u[s[i] - 97] -= 1 ;
}
}
bool f1 = true ;
int po = 0 ;
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 (po == 0 && ne == 0)
cout << ( "YES" ) << endl;
else if (po == 1 && ne == 0)
cout << ( "YES" ) << endl;
else if (po == 0 && ne == -1)
cout << ( "YES" ) << endl;
else
cout << ( "NO" ) << endl;
}
|
Java
public class Improve {
public static void main(String args[])
{
String s = "ABCdcba" ;
int u[] = new int [ 26 ];
int n = s.length() ;
for ( int i = 0 ; i < n ; i++)
{
if (Character.isUpperCase(s.charAt(i)))
{
u[s.charAt(i) - 65 ] += 1 ;
}
else
{
u[s.charAt(i) - 97 ] -= 1 ;
}
}
boolean f1 = true ;
int po = 0 ;
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 (po == 0 && ne == 0 )
System.out.println( "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" ) ;
}
}
|
Python3
s = "ABCdcba"
u = [ 0 ] * 26
n = len (s)
for i in range (n):
if (s[i].isupper()):
u[ ord (s[i]) - 65 ] + = 1
else :
u[ ord (s[i]) - 97 ] - = 1
fl = True
po = 0
ne = 0
for i in range ( 26 ):
if (u[i] > 0 ):
po + = u[i]
if (u[i] < 0 ):
ne + = u[i]
if (po = = 0 and ne = = 0 ):
print ( "YES" )
elif (po = = 1 and ne = = 0 ):
print ( "YES" )
elif (po = = 0 and ne = = - 1 ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public static void Main()
{
string s = "ABCdcba" ;
int [] u = new int [26];
int n = s.Length ;
for ( int i = 0; i < n ; i++)
{
if (Char.IsUpper(s[i]))
{
u[s[i] - 65] += 1 ;
}
else
{
u[s[i] - 97] -= 1 ;
}
}
int po = 0 ;
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 (po == 0 && ne == 0)
Console.Write( "YES" + "\n" ) ;
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" ) ;
}
}
|
Javascript
<script>
let s = "ABCdcba" ;
let u = Array(26).fill(0);
let n = s.length ;
for (let i = 0; i < n ; i++)
{
if (s[i].toUpperCase())
{
u[s[i] - 65] += 1 ;
}
else
{
u[s[i] - 97] -= 1 ;
}
}
let f1 = true ;
let po = 0 ;
let ne = 0 ;
for (let i = 0 ; i < 26 ; i++)
{
if (u[i] > 0)
po += u[i] ;
if (u[i] < 0)
ne += u[i] ;
}
if (po == 0 && ne == 0)
document.write( "YES" ) ;
else if (po == 1 && ne == 0)
document.write( "YES" ) ;
else if (po == 0 && ne == -1)
document.write( "YES" ) ;
else
document.write( "NO" ) ;
</script>
|
Time complexity: O(n)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...