3ms 5 lines concise C++ solution


  • 2
    M
        int longestPalindrome(string s) {
            if (s.size() <= 1) return s.size();
            int count = 0, list[256] = { 0 };
            for (auto& c : s) list[c - '\0']++;
            for (auto& n : list) count += (n % 2)? n - 1 : n;
            return (count < s.size()) ? count + 1 : count;
        }

  • 0

    @mufeng excellent! by checking whether count < s.size(), one can tell if there is odd number of character exists. If no odd number, all the character are made use of and count should equal the size of s.


Log in to reply
 

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