Skip to main content

Longest Palindrome Length

Also known as: Longest Palindrome

Find the length of the longest palindrome that can be built Example: "abccccdd" => 7 (can build "dccaccd")

Given a string s containing 1 to 2000 uppercase or lowercase English letters, return the integer length of the longest palindrome that can be formed using some or all of its letters. Letters may be rearranged, but each occurrence can be used at most once. Letter case matters, so "A" and "a" are different characters. For example, "abccccdd" returns 7.

Examples

You write

longest_palindrome(s: str)

  1. Example 1

    Input
    s = "abccccdd"
    returns
    7
  2. Example 2

    Input
    s = "a"
    returns
    1

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Part 1: Longest Palindrome Lengtheasy