C++ Bitmap for Odds 3ms


  • 0
    K

    The idea is to keep toggle the corresponding bit representing the character.
    Leverage C++ bitset to speed up the counting of bits get set.

    class Solution {
    public:
        int longestPalindrome(string s) {
            bitset<128> odds;
            
            for (char c: s) {
                odds[c].flip();
            }
    
            // total = length of s - any non even part (odds) except one
            return s.size() - odds.count() + odds.any();
        }
    };
    

Log in to reply
 

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