Skip to content

Instantly share code, notes, and snippets.

@yarax
Last active August 29, 2015 14:22
Show Gist options
  • Save yarax/a6d262a1c9b093d5c998 to your computer and use it in GitHub Desktop.
Save yarax/a6d262a1c9b093d5c998 to your computer and use it in GitHub Desktop.
Finding anagrams
var row = "word".split("");
// splice modifies original array
function pull (row, i) {
var arr = [];
row.forEach(function (item, j) {
if (i!=j) arr.push(item);
});
return arr;
}
function comb(row) {
if (row.length === 2) return [row[0]+row[1], row[1]+row[0]];
var myRow = [];
for (var i =0; i<row.length; i++){
var pulled = pull(row,i);
var res = comb(pulled);
for (var m=0; m<res.length; m++) {
myRow.push(row[i] + res[m]);
}
}
return myRow;
}
console.log(comb(row));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment