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

#include <stdio.h>
#include <string.h>

int main() {
    char str[1000];
    scanf("%s", str);
    int hash[26] = {0};
    int count = 0, c2 = 0;

    for(int i = 0; str[i] != ''; i++) {
        if(str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u') {
            hash[str[i] - 'a']++;
        }
    }

    for(int i = 0; i < 26; i++) {
        count += (hash[i] / 2) * 2;
        if(hash[i] % 2 == 1) {
            c2 = 1;
        }
    }

    printf("%d", count + c2);
    return 0;
}
from collections import Counter

str = input().strip()
count = c2 = 0
hash = Counter([ch for ch in str if ch not in 'aeiou'])

for key, value in hash.items():
    count += (value // 2) * 2
    if value % 2 == 1:
        c2 = 1

print(count + c2)
import java.util.HashMap;
import java.util.Scanner;

public class MaxPalindromeLength {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        HashMap<Character, Integer> hash = new HashMap<>();
        int count = 0, c2 = 0;

        for(char ch : str.toCharArray()) {
            if("aeiou".indexOf(ch) == -1) {
                hash.put(ch, hash.getOrDefault(ch, 0) + 1);
            }
        }

        for(int value : hash.values()) {
            count += (value / 2) * 2;
            if(value % 2 == 1) {
                c2 = 1;
            }
        }

        System.out.println(count + c2);
    }
}
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    string str;
    cin >> str;
    map<char, int> hash;
    int count = 0, c2 = 0;

    for(char ch : str) {
        if(string("aeiou").find(ch) == string::npos) {
            hash[ch]++;
        }
    }

    for(auto &pair : hash) {
        count += (pair.second / 2) * 2;
        if(pair.second % 2 == 1) {
            c2 = 1;
        }
    }

    cout << count + c2;
    return 0;
}
Previous Article

Function tenthDigitProduct - CTS PATTERN

Next Article

Employees - Working Time

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *