Find the Longest Palindrome Length From Available Letters
Quick Overview
Practice finding the maximum palindrome length obtainable by rearranging case-sensitive English letters. Respect occurrence limits and distinguish this task from substring search.
Find the Longest Palindrome Length From Available Letters
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
# Find the Longest Palindrome Length From Available Letters
Given a string `s` of uppercase and lowercase English letters, return the greatest length of a palindrome that can be built from its letters. You may rearrange the letters and leave some unused, but cannot use an occurrence more than once.
Letter case matters: `A` and `a` are different characters.
### Function
`int longestPalindrome(string s)`
### Example 1
```text
Input: s = "aaBBBccD"
Output: 7
```
For example, `"aBcDcBa"` uses two `a`s, two `B`s, two `c`s, and one `D`. No palindrome can use all eight letters of this input.
### Example 2
```text
Input: s = "AaBb"
Output: 1
```
Each character occurs once, and case-sensitive matching cannot create a pair.
### Constraints
- `1 <= s.length <= 2000`.
- `s` contains uppercase and/or lowercase English letters only.
### Clarifications
Return a length, not a constructed palindrome. This is a rearrangement problem, so the selected occurrences need not be contiguous or retain their input order. A one-character string is a palindrome.
### What a Strong Answer Covers
- Case sensitivity and occurrence counts.
- Strings with only paired letters or many unmatched letters.
- Why the returned length is achievable and no longer palindrome is possible.
- Correctness and resource bounds.
Overview: Practice finding the maximum palindrome length obtainable by rearranging case-sensitive English letters. Respect occurrence limits and distinguish this task from substring search.
Find the Longest Palindrome Length From Available Letters
Amazon
easySoftware EngineerOtherCoding & Algorithms
0
0
Find the Longest Palindrome Length From Available Letters
Given a string s of uppercase and lowercase English letters, return the greatest length of a palindrome that can be built from its letters. You may rearrange the letters and leave some unused, but cannot use an occurrence more than once.
Letter case matters: A and a are different characters.
Function
int longestPalindrome(string s)
Example 1
Input: s = "aaBBBccD"
Output: 7
For example, "aBcDcBa" uses two as, two Bs, two cs, and one D. No palindrome can use all eight letters of this input.
Example 2
Input: s = "AaBb"
Output: 1
Each character occurs once, and case-sensitive matching cannot create a pair.
Constraints
1 <= s.length <= 2000
.
s
contains uppercase and/or lowercase English letters only.
Clarifications
Return a length, not a constructed palindrome. This is a rearrangement problem, so the selected occurrences need not be contiguous or retain their input order. A one-character string is a palindrome.
What a Strong Answer Covers Guidance
Case sensitivity and occurrence counts.
Strings with only paired letters or many unmatched letters.
Why the returned length is achievable and no longer palindrome is possible.