Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Longest Palindrome problem solution

YASH PAL, 31 July 2024

In this Leetcode Longest Palindrome problem solution, You are given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, “Aa” is not considered a palindrome here.

Leetcode Longest Palindrome problem solution

Problem solution in Python.

class Solution:
    def longestPalindrome(self, s: str) -> int:
        a = set()
        for l in s:
            if l not in a:
                a.add(l)
            else:
                a.remove(l)
        return len(s) - len(a) + 1 if len(a) else len(s)

Problem solution in Java.

public class Solution {
    public int longestPalindrome(String s) {
        if(s==null|| s.length()==0)
        return 0;
        if(s.length()==1)
        return 1;
        
        int[] alpha=new int[128];
        int max_length=0;
        for(char c:s.toCharArray()){
            alpha[c]++;
            if(alpha[c]==2)
            {
                max_length+=2;
                alpha[c]=0;
            }
        }
        if(s.length()>max_length)
         return max_length+1;
        else
         return max_length;
        
    }
}

Problem solution in C++.

int longestPalindrome(string s) {
        if (s.size() < 2) return s.size();
        int ht[256]{0};
        for (auto a : s) ++ht[a];
        int length{0};
        bool has_odd{false};
        for (int i = 65; i <= 122; ++i) {
            if (ht[i]) {
                length += ht[i];
                if (ht[i] % 2 == 1) {
                    if (!has_odd) has_odd = true;
                    length -= 1;
                }
            }
        }
        if (has_odd) ++length;
        return length;
    }

Problem solution in C.

#include<string.h>
int longestPalindrome(char * s){
    int count[52]={0};
    int len=strlen(s);
    for(int i=0;i<len;i++){
        if(s[i]<='Z' && s[i]>='A'){
            count[26+s[i]-'A']++;
        }else{
            count[s[i]-'a']++;
        }
    }
    int ans=0;
    bool odd=false;
    for(int i=0;i<52;i++){
        if(count[i]%2){
            ans+=(count[i]-1);
            odd=true;  
        }else{
            ans+=count[i];
        }
    }
    return odd?ans+1:ans;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes