Golang solution use bit mask


  • 0
    S
    func longestPalindrome(s string) int {
        var bitMap int64 = 0
        res := 0
        length := len(s)
        for i:=0; i<length; i++ {
            mask := int64(1<<(s[i] - 'A'))
            if (bitMap & mask) != 0 {
                bitMap &= ^mask
                res += 2
            } else {
                bitMap |= mask
            }
        }
        if bitMap!=0 {
            res++
        }
        return res
    }
    

Log in to reply
 

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