亚麻:Longest Palindrome

亚麻 的OA题

代码在leetcode 测试通过; leetcode 链接

https://leetcode.com/problems/longest-palindrome/description/

  

class Solution {
public:
    
    
    int longestPalindrome(string s) {
        if( s.empty () ) return 0;
      
        unordered_map< char, int > res;
        int max =0;
        bool oflag = false;
        
        for (auto& e: s)
            res[e]+=1;
        
        for (auto& e: res){
            if (e.second%2 == 0)
                max += e.second;
            else {
                oflag = true ;
                max += e.second -1;
            }
        }
        if(oflag)
            return max +1;
        else 
            return max;
    }
};

 

posted @ 2018-01-16 15:12  HisonSanDiego  阅读(142)  评论(0编辑  收藏  举报