...

/

Longest Palindrome

Longest Palindrome

Try to solve the Longest Palindrome problem.

Statement

Given a string s that only contains alphabets, return the length of the longest palindrome that may be composed using those letters.

Note: Letters are case-sensitive. Hence, combinations such as “Aa” are not considered palindromes.

Constraints:

  • 11 \leq s.length 103\leq 10^3

  • s consists of lowercase and/or uppercase English letters only.

Examples

canvasAnimation-image
1 / 2

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Longest Palindrome

1.

(Select all that apply.) Which options are valid palindromes that can be made using the letters in the string given below?

string = “abbjhhrrssS” Multi-select

A.

“rhbsSsbhr”

B.

"bhrsjsjsrhb”

C.

“bhrsSsrhb”

D.

“SsrhbjbhrsS”


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

We have left the solution to this challenge as an exercise for you. The optimal solution to this problem runs in O(n) time and takes O(n) space. You may try to translate the logic of the solved puzzle into a coded solution.

Python
usercode > main.py
def longest_palindrome(s):
# Replace this placeholder return statement with your code
return -1
Longest Palindrome

Access this course and 1200+ top-rated courses and projects.