problem no 31: string problem:Anagram palindrome (VIP).
QUESTION:
Given a string S, Check if characters of the given string can be rearranged to form a palindrome.
For example characters of “geeksogeeks” can be rearranged to form a
palindrome “geeksoskeeg”, but characters of “geeksforgeeks” cannot be
rearranged to form a palindrome.
Input:
First line consists of integer T denoting the number of test cases. T testcases follow. For each testcase there are one line of input containing string S.
Output:
For each testcase, in a new line, print "Yes" if is possible to make it a palindrome, else "No".
Constraints:
1 <= T <= 100
1 <= |S| <= 1000
Example:
Input:
2
geeksogeeks
geeksforgeeks
Output:
Yes
No
ANSWER:
#include <iostream>
#include<cstring>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int arr[128];
for(int i=0;i<128;i++)
{
arr[i]=0;
}
if(s.size()%2==0)
{
for(int i=0;i<s.size();i++)
{
arr[(int)s[i]]= arr[(int)s[i]]+1;
}
int countofoddnumber=0;
for(int i=0;i<128;i++)
{
if((arr[i]%2)!=0)
{
countofoddnumber+=1;
}
}
if(countofoddnumber>0)
{
cout<<"No\n";
}
else
{
cout<<"Yes\n";
}
}
else
{
for(int i=0;i<s.size();i++)
{
arr[(int)s[i]]= arr[(int)s[i]]+1;
}
int countofoddnumber=0;
for(int i=0;i<128;i++)
{
if((arr[i]%2)!=0)
{
countofoddnumber+=1;
}
}
if(countofoddnumber==1)
{
cout<<"Yes\n";
}
else
{
cout<<"No\n";
}
}
}
return 0;
}
Comments
Post a Comment