C++ Solution by Counting Letters, O(N) Time, O(1) Space, 3 ms


  • 0
    M
    class Solution {
    public:
        int longestPalindrome(string s) {
            vector<int> cnt(128);
            for (char c: s) ++cnt[c];
            int len = 0;
            for (int n: cnt) len += n - n % 2;
            return len + (s.size() > len ? 1 : 0);
        }
    };
    

Log in to reply
 

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