Created
June 4, 2024 03:58
-
-
Save shitu13/d0ba17032517318ad0aaa339bfeb3d0a to your computer and use it in GitHub Desktop.
Longest Palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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