3ms c++ solution using hash table


  • 1
    H
    class Solution {
    public:
        int longestPalindrome(string s) {
            int rep[58] = {0};
            for (char c : s) {
                rep[(int)(c - 'A')]++;
            }
            int out = 0, odd = 0;
            for (int i = 0; i < 58; i++) {
                if (rep[i] % 2 == 0) out += rep[i];
                else {
                    out += (rep[i] - 1);
                    odd ++;
                }
            }
            return (odd > 0 ? out + 1 : out);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.