-
Notifications
You must be signed in to change notification settings - Fork 0
/
409-longest-palindrome.cpp
31 lines (26 loc) · 1.03 KB
/
409-longest-palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Title: Longest Palindrome
// Description:
// 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 here.
// Link: https://leetcode.com/problems/longest-palindrome/
// Time complexity: O(n)
// Space complexity: O(1)
class Solution {
public:
int longestPalindrome(std::string s) {
std::unordered_map<char, std::size_t> charCounts;
// count each char
for (char c: s) ++charCounts[c];
int result = 0;
bool hasUnpairedChar = false;
for (const auto &[key, val]: charCounts) {
// take only paired chars
result += val - (val % 2);
// record if there is any unpaired char
if (val % 2 != 0) hasUnpairedChar = true;
}
// an extra unpaired char can be put in the middle of a palindrome
if (hasUnpairedChar) result += 1;
return result;
}
};