Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created June 4, 2024 03:58
Show Gist options
  • Save shitu13/d0ba17032517318ad0aaa339bfeb3d0a to your computer and use it in GitHub Desktop.
Save shitu13/d0ba17032517318ad0aaa339bfeb3d0a to your computer and use it in GitHub Desktop.
Longest Palindrome
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<int, int> mp;
for (char& ch : s)
mp[ch]++;
bool odd_cnt = false;
int res = 0;
for (auto& it : mp) {
if (it.second % 2 == 0)
res += it.second;
else {
odd_cnt = true;
res += it.second - 1;
}
}
if (odd_cnt)
return res += 1;
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment