Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 28, 2018 07:38
Show Gist options
  • Select an option

  • Save lienista/b4a51577a7dbad67a8e33d379f5e8f23 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/b4a51577a7dbad67a8e33d379f5e8f23 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.4 Palindrome Permutation: Given a string, determine if it is a permutation of a palindrome.
const isPalindrome = (s1) => {
let charMap = new Map();
for(let i=0; i <s1.length; i++){
if(charMap.get(s1[i])) {
charMap.delete(s1[i]);
} else {
charMap.set(s1[i], 1);
}
}
if(s1.length%2 === 0 && charMap.size === 0 ||
s1.length%2 === 1 && charMap.size === 1) {
return true;
}
return false;
}
let x = 'abbc';
console.log(isPalindrome(x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment