Created
July 28, 2017 20:56
-
-
Save robbiemu/8ff5bda4ff4d31adb5eeef1a814ddb64 to your computer and use it in GitHub Desktop.
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
function splitAllWays(result, left, right){ | |
result.push(left.concat(right)); | |
if (right.length > 1){ | |
for(var i = 1; i < right.length; i++){ | |
splitAllWays(result, left.concat(right.substring(0, i)), right.substring(i)); | |
} | |
} | |
return result; | |
}; | |
function permutationsOfBase(str,sets) { | |
let base = Array.from(new Set(str.split(''))) | |
let out = [] | |
base.forEach((x,i) => { | |
base.forEach((y,j) => { | |
if (i === j) return | |
let xCount = (str.match(new RegExp(x,'g')) || []).length | |
let yCount = (str.match(new RegExp(y,'g')) || []).length | |
sets.forEach(set => { | |
set.forEach(word => { | |
let cxC = xCount | |
let cyC = yCount | |
let arranged = word.split('').map(l => { | |
if(l === x && cyC) { | |
l = y | |
cyC-- | |
} else if(l === y && cxC) { | |
l = x | |
cxC-- | |
} | |
return l | |
}).join('') | |
out.push(arranged, word) | |
}) | |
}) | |
}) | |
}) | |
return Array.from(new Set(out)) | |
} | |
var str = "3123"; | |
permutationsOfBase(str,splitAllWays([], [], str)).sort() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment