如何生成具有特定长度限制的所有可能组合中的字母?在javascript中
// return [aaaa, bbbb]
function allPossibleCombinations(inputArray, outputEachStrLength) {
var inputArrayLength = inputArray.length;
for (i = 0; i < inputArrayLength; i++) {
}
}
var inputArray = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z];
var outputEachStrLength = 4;
allPossibleCombinations(inputArray, outputEachStrLength)
答案 0 :(得分:8)
您可以使用递归方法:
function allPossibleCombinations(input, length, curstr) {
if(curstr.length == length) return [ curstr ];
var ret = [];
for(var i = 0; i < input.length; i++) {
ret.push.apply(ret, allPossibleCombinations(input, length, curstr + input[i]));
}
return ret;
}
var input = [ 'a', 'b', 'c', 'd' ];
console.log(allPossibleCombinations(input, 3, ''));