Skip to content
Related Articles

Related Articles

Improve Article

Check if a string can be rearranged to form special palindrome

  • Difficulty Level : Easy
  • Last Updated : 21 May, 2021
Geek Week

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 lower case 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.
Below is the implementation of the above approach: 
 

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[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




<script>
 
// JavaScript implementation of the approach
 
// driver code
 
     let s = "ABCdcba" ;
           
        // creating a array which stores the 
        // frequency of each character
        let u = Array(26).fill(0);
        let n = s.length ;
           
        for(let i = 0; i < n ; i++)
        {
            // Checking if a character is
            // uppercase or not
            if(s[i].toUpperCase())
            {
                // Increasing by 1 if uppercase
                u[s[i] - 65] += 1 ;
            }
            else
            {
                // Decreasing by 1 if lower case
                u[s[i] - 97] -= 1 ;
            }
               
        }
        let f1 = true ;
           
        // Storing the sum of positive 
        // numbers in the frequency array
        let po = 0 ;
           
        // Storing the sum of negative 
        // numbers in the frequency array
        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 all character balances out then its Yes
        if (po == 0 && ne == 0)
            document.write("YES") ;
           
        // If there is only 1 character which 
        // does not balances then also it is Yes
        else if (po == 1 && ne == 0)
            document.write("YES") ;
           
        else if (po == 0 && ne == -1)
            document.write("YES") ;
           
        else
            document.write("NO") ;
   
</script>
Output: 
YES

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :