get all combinations for a string

前端 未结 8 1688
傲寒
傲寒 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:33

    The accepted solution as of July 29, 2019 allows for duplicate strings to be returned in the array. The following adjustment fixes that little issue by adding a condition to the push.

                // however, we don't want duplicates so...
                if (!result.includes(next)) {
                    result.push(next);    
                }
    

提交回复
热议问题