-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2131.cpp
More file actions
27 lines (25 loc) · 716 Bytes
/
Copy path2131.cpp
File metadata and controls
27 lines (25 loc) · 716 Bytes
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
class Solution {
public:
int longestPalindrome(vector<string> &words) {
unordered_map<string, int> um;
for (string &w : words)
um[w]++;
bool odd = false;
int res = 0;
for (const auto &[s, count] : um) {
if (!count) continue;
if (s[0] == s[1]) {
if (count % 2 == 0) {
res += count;
} else {
res += count - 1;
odd = true;
}
} else if (s[0] < s[1] && um.count({s[1], s[0]})) {
res += min(count, um[{s[1], s[0]}]) * 2;
}
}
if (odd) res++;
return res * 2;
}
};