给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
在构造过程中,请注意区分大小写。比如 “Aa” 不能当做一个回文字符串。
注意:
假设字符串的长度不会超过 1010。
示例 1:
输入:
“abccccdd”
输出:
7
思路分析
/*** @param {string} s* @return {number}*/var longestPalindrome = function (s) {// 统计各个字母出现的次数const map = new Map();const len = s.length;for (let i = 0; i < len; i++) {map.set(s[i], (map.get(s[i]) || 0) + 1);}let res = 0;// 遍历mapfor (const item of map) {// res累加上 出现次数-次数对2取模res += item[1] - (item[1] % 2);}// 如果有奇数字母的,res加1if (res < len) res++;return res;};
