Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Example 1:
Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000s consists of lowercase and/or uppercase English letters only.Problem summary: Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Greedy
"abccccdd"
"a"
palindrome-permutation)longest-palindrome-by-concatenating-two-letter-words)largest-palindromic-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #409: Longest Palindrome
class Solution {
public int longestPalindrome(String s) {
int[] cnt = new int[128];
int n = s.length();
for (int i = 0; i < n; ++i) {
++cnt[s.charAt(i)];
}
int ans = 0;
for (int v : cnt) {
ans += v / 2 * 2;
}
ans += ans < n ? 1 : 0;
return ans;
}
}
// Accepted solution for LeetCode #409: Longest Palindrome
func longestPalindrome(s string) (ans int) {
cnt := [128]int{}
for _, c := range s {
cnt[c]++
}
for _, v := range cnt {
ans += v / 2 * 2
}
if ans < len(s) {
ans++
}
return
}
# Accepted solution for LeetCode #409: Longest Palindrome
class Solution:
def longestPalindrome(self, s: str) -> int:
cnt = Counter(s)
ans = sum(v // 2 * 2 for v in cnt.values())
ans += int(ans < len(s))
return ans
// Accepted solution for LeetCode #409: Longest Palindrome
use std::collections::HashMap;
impl Solution {
pub fn longest_palindrome(s: String) -> i32 {
let mut cnt = HashMap::new();
for ch in s.chars() {
*cnt.entry(ch).or_insert(0) += 1;
}
let mut ans = 0;
for &v in cnt.values() {
ans += (v / 2) * 2;
}
if ans < (s.len() as i32) {
ans += 1;
}
ans
}
}
// Accepted solution for LeetCode #409: Longest Palindrome
function longestPalindrome(s: string): number {
const cnt: Record<string, number> = {};
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
}
let ans = Object.values(cnt).reduce((acc, v) => acc + Math.floor(v / 2) * 2, 0);
ans += ans < s.length ? 1 : 0;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.