Longest Palindrome Length
The program must accept a string S as the input. The program must print the length of the longest palindrome that can be formed with the consonants in the string S as the output.
Note:
At least one consonant must be present in the string S.
The string S contains only lower case alphabets.
Note:
At least one consonant must be present in the string S.
The string S contains only lower case alphabets.
Boundary Condition(s):
1 <= Length of S <= 10^4
1 <= Length of S <= 10^4
Input Format:
The first line contains the string S.
The first line contains the string S.
Output Format:
The first line contains the length of the longest palindrome in the string S.
The first line contains the length of the longest palindrome in the string S.
Example Input/Output 1:
Input:
abcdeedb
Input:
abcdeedb
Output:
5
5
Explanation:
The longest palindrome can be formed with bbddc (The order can be any but the length is 5).
The longest palindrome can be formed with bbddc (The order can be any but the length is 5).
Example Input/Output 2:
Input:
racecar
Input:
racecar
Output:
4
4
#include<stdio.h>
#include <stdlib.h>
int main()
{
char str[1000];
scanf("%s",str);
int hash[26]={0};
int count=0,c2=0;
int len=strlen(str);
for(int i=0;i<len;i++)
{
hash[str[i]-'a']++;
}
for(int i=0;i<26;i++)
{
if(i!=0 && i!=4 && i!=8 && i!=14 && i!=20)
{
int x=hash[i];
while(x>=2)
{
count+=2;
x=x-2;
}
if(x==1)
{
c2=1;
}
}
}
printf("%d",count+c2);
}
Comments
Post a Comment