Search code examples
javascriptcartesian

Generating all possible combinations of strings


I am trying to generate all possible combinations of a string.

e.g. for the list below: a1q5z!H9, b1q5z!H9, c1q5z!H9, d1q5z!H9, a2q5z!H9 ... etc

Rather than make lots of nested loops, I thought I would try something clever with MODULO ... but hit a wall.

This is the Javascript I have come up with - any pointers to how I might go on?

var c = [
  ['a', 'b', 'c', 'd'],
  ['1', '2', '3', '4'],
  ['q', 'w', 'e', 'r'],
  ['5', '6', '7', '8'],
  ['z', 'x', 'c', 'v'],
  ['!', '"', '£', '$'],
  ['H', 'J', 'K', 'L'],
  ['9', '8', '7', '6'],
];

var o = document.getElementById('output');
var pw = "";
var chars = c.length;

for( var i = 0; i <20; i++)
{
  pw = ""
  for(var j = 0; j < chars; j++ )
    {
      pw += c[j][i%4];
    }
  op(pw);
}

function op(s)
{
  o.innerHTML = o.innerHTML + "<br>" + s;
}

This just outputs the first 20 in the list, but repeats ... I nearly have it but not quite. Any help or pointers appreciated.


Solution

  • Quite easy to write a recursive function demo.

    function permutate(abc, memo) {
        var options;
        memo = memo || abc.shift().slice(0);
    
        if(abc.length) {
            options = abc.shift();
    
            return permutate(abc, memo.reduce(function(all, item){
                return all.concat(options.map(function(option){
                    return item + option;
                }))
            }, []));       
        }
    
        return memo;
    };
    
    console.log(permutate(c).length); //65536 items
    

    Or more imperative approach

    function permutate2(abc) {
        var options, i, len, tmp, j, optionsLen, 
            memo = abc.pop().slice(0); //copy first the last array
    
        while(options = abc.pop()) { //replace recursion
            tmp = [];
            optionsLen = options.length;
            for(i = 0, len = memo.length; i < len; i++) { //for every element in memo
                for(j = 0; j < optionsLen; j++) { //do cartesian product with options
                    tmp.push(options[j] + memo[i]);    
                }
            }
            memo = tmp;
        }
    
        return memo;
    }