Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save lienista/d3d8435f0fb36c168fb647889d901b57 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 isPermutation = (s1, s2) => {
if(s1.length != s2.length) return false;
let count = {};
for(let i=0; i < s1.length; i++) {
count[s1[i]] = count[s1[i]] === undefined ? 1: count[s1[i]] + 1;
count[s2[i]] = count[s2[i]] === 0 ? 0 : count[s2[i]] - 1;
}
for(let j in count){
if(count[j] > 0)
return false;
}
return true;
}
var a = isPermutation("paypalishiring", "palpayhiringis"); // true
var b = isPermutation("paypalishiring", "hiringpalsipay"); // true
var c = isPermutation("paypalishiring", "palpayhirising"); // true
var d = isPermutation("paypalishiring", "palpayhiringgg"); // false
console.log(a + ', ' + b + ', ' + c + ', ' + d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment