Simple C++ hash


  • 0
    F

    Pretty much the same idea as everyone else.

    class Solution {
    public:
        int longestPalindrome(string s) {
            int hash[256] = {0};
            int result = 0;
            for(auto ch : s){
                hash[ch]++;
            }
            bool haveOdd = false;
            for(int i = 0; i < 256; ++i){
                if(hash[i]%2==0){
                    result += hash[i];    
                }else if(hash[i] > 0){
                    result += hash[i]-1;
                    haveOdd = true;
                }
            }
            if (haveOdd) ++result;
            return result;
        }
    };
    

Log in to reply
 

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