First, characters are counted. Even occurring characters (v[i]%2 == 0) can always be used to build a palindrome. For every odd occurring character (v[i]%2 == 1), v[i]-1 characters can be used. Res is incremented if there is at least one character with odd occurrence number.

```
int longestPalindrome(string s) {
vector<int> v(256,0);
for(int i = 0; i < s.size(); ++i)
++v[s[i]];
int res = 0;
bool odd = false;
for(int i = 0; i < 256; ++i)
if(v[i]%2 == 0)
res += v[i];
else
{
res += v[i] - 1;
odd = true;
}
if(odd)
++res;
return res;
}
```