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.
Boundary Condition(s):
1 <= Length of S <= 10^4
Input Format:
The first line contains the string S.
Output Format:
The first line contains the length of the longest palindrome in the string S.
Example Input/Output 1:
Input:
abcdeedb
Output:
5
Explanation:
The longest palindrome can be formed with bbddc (The order can be any but the length is 5).
Example Input/Output 2:
Input:
racecar
Output:
4


Solution:-

#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

Popular posts from this blog

Two Matrix Spiral Print

Alphabets Positions Reversed

Odd Factors - (Error Identification) skillrack program id - 7306