Last active
August 28, 2018 07:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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