An
anagram of a string is another string that contains the same characters, only the order of characters can be different.
So here we have to check whether rearranging string characters can make the string as palindrome or not
Steps to get the solution :
1.Loop over the characters of string and keep the count in exists Object
2.Check how any characters have odd count using variable oddCount
3.If string length is even and oddCount is 0, then it can be made as palindrome e.g maam
4.If string length is odd and oddCount is 1, then it can be made a palidrome e.g madam
function checkPalindromeForAnagram(str) {
const exists={};
let oddCount=0;
for(let i=0;i<str.length;i++) {
if(exists[str[i]]) {
exists[str[i]]++;
}
else {
exists[str[i]]=1;
}
}
for(let key in exists) {
if(exists[key]%2!==0) {
oddCount++;
}
}
if((str.length%2==0 && oddCount ==0) || (str.length%2!==0 && oddCount == 1)) {
return true;
}
else {
return false;
}
}
let str = 'adamm';
let str2= 'caat';
let result = checkPalindromeForAnagram(str);
let result2 = checkPalindromeForAnagram(str2)
console.log('result', result); //result will be true as rearranging string 'adamm' can be made as madam which is a palindrome
console.log('result2', result2); //result will be false as rearranging string 'caat' cant be made as palindrome