Last active
August 29, 2015 14:22
-
-
Save yarax/a6d262a1c9b093d5c998 to your computer and use it in GitHub Desktop.
Finding anagrams
This file contains 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
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