Given a string S consisting of N lowercase alphabets, the task is to check if all the strings of at least length 2 formed by selecting every character of the string S only once are palindromic or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “abbbaddzcz”
Output: Yes
Explanation: The palindromic strings of length greater than 1 are { “aa”, “bbb”, “dd”, “zcz”} and all the characters in the given string is used only once. Therefore, print “Yes”.
Input: S = “abcd”
Output: No
Approach: The idea is to break the string into a palindromic strings of even length, and if there exists a character having frequency 1, then pair it with a palindromic string of even length. Follow the steps below to solve the problem:
- Initialize an array, say freq[], of size 26, to store the frequency of each character present in the string.
- Iterate over the characters of the given string S and update the frequency of each character in the array freq[].
- Initialize two variables, say O and E, to store the count of unique characters and characters having even frequencies respectively.
- Traverse the array freq[] and if freq[i] is equal to 1, then increment O by 1. Otherwise, increment E by 1.
- Check if E ? O, then print “Yes”. Otherwise, perform the following steps:
- Update O to O – E, to store the remaining unique characters after pairing.
- Traverse the array freq[] and if the value of O is at most 0, then break out of the loop. Otherwise, update O to O – (freq[i]/2) and then increment O by 1.
- After completing the above steps, if the value of O ? 0, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void checkPalindrome(string& s)
{
int a[26] = { 0 };
int o = 0, e = 0;
for ( int i = 0; s[i] != '\0' ; i++)
a[( int )s[i] - 97]++;
for ( int i = 0; i < 26; i++) {
if (a[i] == 1)
o++;
else if (a[i] % 2 == 0
and a[i] != 0)
e += (a[i] / 2);
}
if (e >= o)
cout << "Yes" ;
else {
o = o - e;
for ( int i = 0; i < 26; i++) {
if (o <= 0)
break ;
if (o > 0
and a[i] % 2 == 1
and a[i] > 2) {
int k = o;
o = o - a[i] / 2;
if (o > 0 or 2 * k + 1 == a[i]) {
o++;
a[i] = 1;
}
}
}
if (o <= 0)
cout << "Yes" ;
else
cout << "No" ;
}
}
int main()
{
string S = "abbbaddzcz" ;
checkPalindrome(S);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void checkPalindrome(String s)
{
int a[] = new int [ 26 ];
int o = 0 , e = 0 ;
for ( int i = 0 ; i < s.length(); i++)
a[s.charAt(i) - 'a' ]++;
for ( int i = 0 ; i < 26 ; i++)
{
if (a[i] == 1 )
o++;
else if (a[i] % 2 == 0 && a[i] != 0 )
e += (a[i] / 2 );
}
if (e >= o)
System.out.println( "Yes" );
else
{
o = o - e;
for ( int i = 0 ; i < 26 ; i++)
{
if (o <= 0 )
break ;
if (o > 0 && a[i] % 2 == 1 && a[i] > 2 )
{
int k = o;
o = o - a[i] / 2 ;
if (o > 0 || 2 * k + 1 == a[i])
{
o++;
a[i] = 1 ;
}
}
}
if (o <= 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
public static void main(String[] args)
{
String S = "abbbaddzcz" ;
checkPalindrome(S);
}
}
|
Python3
def checkPalindrome(s):
a = [ 0 ] * 26
o, e = 0 , 0
for i in s:
a[ ord (i) - 97 ] + = 1
for i in range ( 26 ):
if (a[i] = = 1 ):
o + = 1
elif (a[i] % 2 = = 0 and a[i] ! = 0 ):
e + = (a[i] / / 2 )
if (e > = o):
print ( "Yes" )
else :
o = o - e
for i in range ( 26 ):
if (o < = 0 ):
break
if (o > 0 and a[i] % 2 = = 1 and a[i] > 2 ):
k = o
o = o - a[i] / / 2
if (o > 0 or 2 * k + 1 = = a[i]):
o + = 1
a[i] = 1
if (o < = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S = "abbbaddzcz"
checkPalindrome(S)
|
C#
using System;
class GFG{
static void checkPalindrome( string s)
{
int [] a = new int [26];
int o = 0, e = 0;
for ( int i = 0; i < s.Length; i++)
a[s[i] - 'a' ]++;
for ( int i = 0; i < 26; i++)
{
if (a[i] == 1)
o++;
else if (a[i] % 2 == 0 && a[i] != 0)
e += (a[i] / 2);
}
if (e >= o)
Console.WriteLine( "Yes" );
else
{
o = o - e;
for ( int i = 0; i < 26; i++)
{
if (o <= 0)
break ;
if (o > 0 && a[i] % 2 == 1 && a[i] > 2)
{
int k = o;
o = o - a[i] / 2;
if (o > 0 || 2 * k + 1 == a[i])
{
o++;
a[i] = 1;
}
}
}
if (o <= 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
static void Main()
{
string S = "abbbaddzcz" ;
checkPalindrome(S);
}
}
|
Javascript
<script>
function checkPalindrome( s )
{
let a = [0]
let o = 0, e = 0;
for (let i = 0; i<s.length; i++)
a[s.charCodeAt(i) - 97]++;
for (let i = 0; i < 26; i++) {
if (a[i] == 1)
o++;
else if (a[i] % 2 == 0 && a[i] != 0)
e += (a[i] / 2);
}
if (e >= o)
document.write( "Yes" )
else {
o = o - e;
for (let i = 0; i < 26; i++)
{
if (o <= 0)
break ;
if (o > 0 && a[i] % 2 == 1 && a[i] > 2)
{
let k = o;
o = o - a[i] / 2;
if (o > 0 || 2 * k + 1 == a[i])
{
o++;
a[i] = 1;
}
}
}
if (o <= 0)
document.write( "Yes" )
else
document.write( "No" )
}
}
let S = "abbbaddzcz" ;
checkPalindrome(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)