c++ O(n) solution


  • 0
    W
    class Solution {
    public:
    	int longestPalindrome(string s) {
    		unordered_map<char, int> charcount;
    		for (char c : s) charcount[c] += 1;
    		int res = 0;
    		int left = 0;
    		for (auto& p : charcount) {
    			if (p.second % 2 == 0) {
    				res += p.second;
    			}
    			else {
    				res += (p.second - 1);
    				left += 1;
    			}
    		}
    		if (left) res += 1;
    		return res;
    	}
    };
    

Log in to reply
 

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