Files
leetcode/greed/409.h
2025-09-15 21:12:04 +08:00

20 lines
495 B
C++

#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char, int> count;
int ans = 0;
for (char c : s)
++count[c];
for (auto p : count) {
int v = p.second;
ans += v / 2 * 2;
if (v % 2 == 1 and ans % 2 == 0)
++ans;
}
return ans;
}
};