get all combinations for a string

前端 未结 8 1689
傲寒
傲寒 2020-12-01 19:34

I\'m trying to create a function in JavaScript that given a string will return an array of all possible combinations of the letters with each used at most once, starting wit

8条回答
  •  北海茫月
    2020-12-01 20:18

    This is what I ended up using.

    var combinations = function (string)
    {
        var result = [];
    
        var loop = function (start,depth,prefix)
        {
            for(var i=start; i 0)
                    loop(i+1,depth-1,next);
                else
                    result.push(next);
            }
        }
    
        for(var i=0; i

提交回复
热议问题