Open jtkaufman737 opened 5 years ago
https://repl.it/@jtkaufman737/QuickwittedMundaneWatchdog
let palindrome = "taco cat"; // should pass
let palindrome2 = "aa"; // should pass
let palindrome3 = "ra ce car"; // should pass
let palindrome4 = "si de car"; // should fail
function isPalindrome(str) {
let keys = {}, vals = [];
Array.from(str.split(" ").join("")).map(m => {
keys[m] = 0; // initial object with letters as keys, each count = 0
})
Array.from(str).map(m => {
keys[m] += 1; // For all array items, increments the count of each letter
})
let odds = Object.values(keys).filter(f => {
return f % 2 === 1; // if MORE THAN ONE odd number is there, it isn't a palindrome
})
odds.length > 1 ? console.log("Sorry, not a palindrome") : console.log("Congrats, its a palindrome!");
}
isPalindrome(palindrome);
isPalindrome(palindrome2);
isPalindrome(palindrome3);
isPalindrome(palindrome4);
https://repl.it/@gauravchl/Palindrome-Permutation
function isPalindrome(str) {
const charCounts = {};
const inputString = str.replace(/ /g, '');
for(let i = 0; i < inputString.length; i++) {
const char = inputString[i].toLowerCase();
charCounts[char] = (charCounts[char] || 0) + 1;
}
const oddsCount = Object
.values(charCounts)
.reduce((a, b) => b % 2 === 1 ? a + 1 : a, 0)
return oddsCount <= 2
}
isPalindrome('asd a sd') // true
isPalindrome('level') // true
isPalindrome('asdfgr') // false
isPalindrome('Tact Coa') // true
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
EXAMPLE Input: Tact Coa Output: True (permutations: "taco cat", "atco eta", etc.)