114. Longest Mirror Build

You have a bag of letter tiles described by the string s; upper case and lower case letters are different tiles. You may pick any of the tiles and arrange them in a row, and you want the row to read the same from left to right and from right to left.

Return the greatest possible length of such a row. Every tile can be used at most once, and a single tile is a valid row. Count how often each letter occurs: pairs of equal tiles go to both ends, and at most one leftover tile can sit in the middle.

Example 1

Input:
s = "xxyzzzyq"
Output:
7
Explanation:

The pairs xx, yy and zz give six tiles, and one more tile (z or q) can go in the middle, so 7.

Example 2

Input:
s = "AaBb"
Output:
1
Explanation:

No letter occurs twice, so only a single tile fits in the row.

Constraints

1 ≤ s.length ≤ 105

s contains only English letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…