Find the Length of the Longest possible palindrome string JavaScript

Given a string s which consists of lowercase or uppercase letters, we are required to 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.

For example, if the input string is "abccccdd", the output should be 7 because one longest palindrome that can be built is "dccaccd", whose length is 7.

Algorithm Explanation

The key insight is that a palindrome can have pairs of characters (which appear on both sides) plus at most one character in the middle. We count character pairs and add 1 if there are leftover characters for the center.

Example

const str = "abccccdd";

const longestPalindrome = (str) => {
    const set = new Set();
    let count = 0;
    
    for (const char of str) {
        if (set.has(char)) {
            count += 2;
            set.delete(char);
        } else {
            set.add(char);
        }
    }
    
    return count + (set.size > 0 ? 1 : 0);
};

console.log(longestPalindrome(str));

Output

7

How It Works

The algorithm uses a Set to track unpaired characters:

  • When we encounter a character already in the set, we found a pair (count += 2) and remove it
  • When we encounter a new character, we add it to the set
  • Finally, if any unpaired characters remain, we can use one as the center (+1)

Step-by-Step Trace

const traceExample = (str) => {
    const set = new Set();
    let count = 0;
    
    console.log(`Processing string: "${str}"`);
    
    for (const char of str) {
        if (set.has(char)) {
            count += 2;
            set.delete(char);
            console.log(`Found pair '${char}': count = ${count}, set = [${Array.from(set).join(', ')}]`);
        } else {
            set.add(char);
            console.log(`Added '${char}': count = ${count}, set = [${Array.from(set).join(', ')}]`);
        }
    }
    
    const result = count + (set.size > 0 ? 1 : 0);
    console.log(`Final: ${count} + ${set.size > 0 ? 1 : 0} = ${result}`);
    return result;
};

traceExample("abccccdd");
Processing string: "abccccdd"
Added 'a': count = 0, set = [a]
Added 'b': count = 0, set = [a, b]
Added 'c': count = 0, set = [a, b, c]
Found pair 'c': count = 2, set = [a, b]
Found pair 'c': count = 4, set = [a, b]
Added 'c': count = 4, set = [a, b, c]
Added 'd': count = 4, set = [a, b, c, d]
Found pair 'd': count = 6, set = [a, b, c]
Final: 6 + 1 = 7

Additional Examples

console.log(longestPalindrome("a"));           // 1 (single character)
console.log(longestPalindrome("bb"));          // 2 (one pair)
console.log(longestPalindrome("ccc"));         // 3 (one pair + center)
console.log(longestPalindrome("abcdeffedcba")); // 13 (all can be paired)
1
2
3
13

Conclusion

This algorithm efficiently finds the longest possible palindrome length by counting character pairs and allowing one unpaired character as the center. The time complexity is O(n) and space complexity is O(k) where k is the number of unique characters.

Updated on: 2026-03-15T23:19:00+05:30

463 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements