C++_Use Vector_3ms_Accepted


  • 0

    The only thing you need to notice is in the ASCII table, 'a' - 'A' = 32, there will be some other chars between Z and a.

    class Solution {
    public:
    int longestPalindrome(string s) {
        if(s.empty()) return 0;
        int n = s.size();
        vector<int> res(52,0);
        for(int i = 0; i < n; i++){
            if(s[i] - 'A' >= 0 && s[i] - 'A' < 26){res[s[i]-'A']++;}
            else{res[s[i]-'a' + 25]++;}
        }
        
        int ans = 0;
        int center = 0;
        for(int j = 0; j < res.size(); j++){
            ans = ans + (res[j]/2)*2;
            if(center == 0 && res[j]%2 ){center = 1;}
        }
        return ans + center;
    }
    };
    

    //'A' - 'a' = -32


Log in to reply
 

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