Strings String basics

129. Longest Palindrome

Easy

You are given a string s of English letters. Build the longest palindrome you can from its letters, and return its length. Each letter can be used at most as many times as it appears in s, and the letters can go in any order. You do not have to use them all.

A palindrome reads the same from left to right and from right to left, such as "level" or "abba". Uppercase and lowercase letters are different letters, so "Aa" is not a palindrome.

Example 1

Input: s = "bananas"

Output: 5

Explanation: "anbna" is one longest palindrome. It uses two of the three as, both ns, and the b in the middle.

Example 2

Input: s = "Noon"

Output: 3

Explanation: N and n are different letters. "oNo" uses both os, with N in the middle.

Example 3

Input: s = "pqrs"

Output: 1

Explanation: No letter appears twice, so the best is a single letter, such as "q".

Check your understanding

Work out the answer for this input before you write any code.

s = "aaabbbcc"

Constraints

  • 1 ≤ s.length ≤ 105
  • s holds lowercase and uppercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.