Longest Palindrome LeetCode solution in C++

1
class Solution {
2
public:
3
int longestPalindrome(string s) {
4
map<char, int> mp;
5
for (int i = 0; i < s.length(); i++) {
6
mp[s[i]]++;
7
}
8
int ans = 0;
9
int odd = 0;
10
for (auto i : mp) {
11
if (i.second % 2 == 1) {
12
odd = 1;
13
}
14
ans = ans + (i.second / 2) * 2;
15
}
16
return ans + odd;
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0