3ms C++


  • 0
    V
    class Solution {
    public:
        int longestPalindrome(string s) {
            
            int result = 0;
            bool flag = false;
    
            if( s.empty() )
                return result;
    
            int charTable[128] = {0};
            for(int i = 0; i < s.size(); ++i) {
                charTable[s[i]]++;
            }
    
            for(int i = 0; i < 128; ++i) {
                if(charTable[i] % 2 == 0) {
                    result += charTable[i];
                }
                else if(charTable[i] > 2) {
                    result += charTable[i] - 1;
                    flag = true;
                } else if( charTable[i] != 0 ) {
                    flag = true;
                }
            }
    
            if(flag)
                return result + 1;
            else
                return result;
        }
    };
    

Log in to reply
 

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