Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

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 solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes