Last active
February 21, 2017 02:48
-
-
Save animatedlew/9e4a6193ee389a831de9b2d30a4314f4 to your computer and use it in GitHub Desktop.
Given two strings, write a method to decide if one is a permutation of the other.
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
function perm(str) { | |
return _perm(str, "").toString().split(','); | |
} | |
function _perm(str, acc) { | |
if (str.length === 0) return acc; | |
else { | |
return str.split('').map((c, i) => { | |
let s = str.substr(0, i) + str.substr(i+1); | |
return _perm(s, acc + str[i]); | |
}); | |
} | |
} | |
function isPerm(str, test) { | |
// over-complicated | |
// return perm(str).includes(test); | |
// simple... | |
return str.split('').sort().join('') == test.split('').sort().join(''); | |
} | |
console.log(isPerm("lewis", "siwel")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment