Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Last active February 21, 2017 02:48
Show Gist options
  • Save animatedlew/9e4a6193ee389a831de9b2d30a4314f4 to your computer and use it in GitHub Desktop.
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.
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