↓Skip to main content
  1. LeetCode/

LeetCode 409: Longest Palindrome

·2 mins· ·
LeetCode Easy Hash-Table String Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-23

  • Submissions: Wrong Answer ×4 → Accepted → Wrong Answer → Accepted

Approach
#

A palindrome is symmetric, so each character contributes at most v // 2 * 2 (an even count). Leftover singles from odd counts can fill only one center slot: if any count is odd, add 1 (Greedy, counting with Hash Table). Case matters.

My Attempts
#

In 2022 it failed four times, always on odd counts:

  • Attempt 1 used v / 2 (a float) and added only half of even counts: "aa" returned 1.0.
  • Attempt 2 added only 1 for an odd count, ignoring its usable v - 1: "bbb" should be 3; it returned 1.
  • Attempt 3 added v - 1 but forgot the center 1: "bbb" returned 2; attempt 4 added v plus 1, giving 4.

The 2026 rewrite had the right logic but wrote return res + 1 if plus_one else 0; the conditional expression binds looser than +, so it meant (res + 1) if plus_one else 0 and returned 0 with no odd counts: "aa" returned 0.

Solution
#

class Solution:
    def longestPalindrome(self, s: str) -> int:
        cnt = Counter(s)
        res = sum(v // 2 * 2 for v in cnt.values())
        return res + (1 if any(v % 2 for v in cnt.values()) else 0)

Complexity
#

Time O(n), space O(k).

Mistakes to Avoid
#

  • An odd count still contributes v - 1, not just one center character.
  • res + 1 if odd else 0 parses as (res + 1) if odd else 0; add parentheses.
  • v / 2 is a float; use //.

Interview Explanation
#

I count characters, use an even number of each on the two sides, and add one center character if any count is odd. Time O(n).

Related

LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes
LeetCode 383: Ransom Note
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 383 note: every character’s demand in ransomNote must not exceed its supply in magazine; an empty Counter difference means yes
LeetCode 387: First Unique Character in a String
·1 min
LeetCode Easy Hash-Table String Queue Counting
LeetCode 387 note: count first, then scan left to right for the first character with count 1
LeetCode 389: Find the Difference
·1 min
LeetCode Easy Hash-Table String Bit-Manipulation Sorting
LeetCode 389 note: t has one extra character; XOR every character of both strings, pairs cancel, and the extra one remains
LeetCode 242: Valid Anagram
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 242 note: two strings are anagrams exactly when their character counts match; compare Counters or sorted strings
LeetCode 205: Isomorphic Strings
·2 mins
LeetCode Easy Hash-Table String
LeetCode 205 note: characters must map one to one; it holds when the distinct characters and distinct pairs all have the same count