leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2131.cpp (716B)
0 class Solution {
1 public:
2 int longestPalindrome(vector<string> &words) {
3 unordered_map<string, int> um;
4 for (string &w : words)
5 um[w]++;
7 bool odd = false;
8 int res = 0;
9 for (const auto &[s, count] : um) {
10 if (!count) continue;
12 if (s[0] == s[1]) {
13 if (count % 2 == 0) {
14 res += count;
15 } else {
16 res += count - 1;
17 odd = true;
18 }
19 } else if (s[0] < s[1] && um.count({s[1], s[0]})) {
20 res += min(count, um[{s[1], s[0]}]) * 2;
21 }
22 }
23 if (odd) res++;
24 return res * 2;
25 }
26 };