Если string.length четное, то Тогда: количество всех символов должно быть четным, поэтому мы не можем иметь символ с нечетным числом.
Если string.length нечетно, то: Количество символов должно быть нечетным, поэтому не все символы должны быть четными.
--------------- Я реализовал следующий JavaScript для последующих ролей:
function isStrPermutationOfPalindrome(_str) { // backward = forward
var isPermutationOfPalindrome = true;
var _strArr = [..._str];
var _strArrLength = _strArr.length;
var counterTable = getCharsTabularFrequencies(_str);
var countOdd = 0;
var countEven = 0;
for (let [ky, val] of counterTable) {
if (val % 2 == 0) {
countEven = countEven + 1;
} else {
countOdd = countOdd + 1;
}
}
if (_strArrLength % 2 == 0) {
//Even count of all characters,otherwise false.
//so can not have a character with odd count.
if (countOdd != 0) {
isPermutationOfPalindrome = false;
}
} else {
//Odd count of 1 character
//so not all chars with even count, only one char of odd count.
if (countOdd > 1 || countOdd == 0) { //no odd, or more than one odd [ only one odd should be to return true]
isPermutationOfPalindrome = false;
}
}
return isPermutationOfPalindrome;
}
function getCharsTabularFrequencies(str) {
str = str.toLowerCase();
var arr = Object.assign([], str);
var oMap = new Map();
var _charCount = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === ' ') {
continue;
}
_charCount = 0;
for (let j = 1; j < arr.length; j++) {
{
if (arr[i] === arr[j]) {
_charCount = _charCount + 1;
}
}
}
if (i == 0)
_charCount = _charCount + 1;
if (!oMap.has(arr[i]))
oMap.set(arr[i], _charCount)
}
return oMap;
}
let _str = 'tactcoapapa';
console.log("Is a string of '" + _str + "' is a permutation of a palindrome ? ANSWER => " + isStrPermutationOfPalindrome(_str));