627. Longest Palindrome

  1. class Solution {
  2. public:
  3. /**
  4. * @param s: a string which consists of lowercase or uppercase letters
  5. * @return: the length of the longest palindromes that can be built
  6. */
  7. int longestPalindrome(string &s) {
  8. // write your code here
  9. int res = 0;
  10. unordered_set <int> set;
  11. for (int i = 0; i < s.size(); i ++){
  12. if (set.count(s[i]) == 0)
  13. set.insert(s[i]);
  14. else{
  15. set.erase(s[i]);
  16. res += 2;
  17. }
  18. }
  19. if (!set.empty()){
  20. res ++;
  21. }
  22. return res;
  23. }
  24. };