Ctrl K

Palindrome From Letter Tiles

Easy
You have a set of letter tiles written as the string tiles: each character is one tile. Using some or all of them, lay out a palindrome, a word that reads the same left to right and right to left. Case matters: A and a are different letters, so Aa is not a palindrome. Return the length of the longest palindrome you can lay out.
Example 1
tiles="rraacce" → 7

For example, racecar: the pairs of r, a and c on the sides and e in the middle use all 7 tiles.

Example 2
tiles="BbbXyy" → 5

Only b and y have pairs, and one leftover tile goes in the middle: for example byXyb, length 5. B and b are different letters and do not pair up.

Example 3
tiles="Q" → 1
Constraints
  • 1 ≤ len(tiles) ≤ 2000
  • tiles consists of lowercase and uppercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEqual Letters Kept Apart HardPieces Without Shared Letters MediumNo Twin Letters Side by Side MediumSplit Letters Into Palindromes MediumDistinct Letter Counts Medium
loading Python…
Input
tiles="rraacce"
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.