Is there any pre-built method for finding all permutations of a given string in JavaScript?

前端 未结 8 2154
遥遥无期
遥遥无期 2020-11-28 10:28

I\'m a newbie to the JavaScript world. As the title mentions, I want to know whether there is any pre-built method in JavaScript to find all possible permutations of a give

8条回答
  •  旧时难觅i
    2020-11-28 10:34

    //string permutation
    
    function permutation(start, string) {
    
        //base case
        if ( string.length == 1 ) {
            return [ start + string ];
        } else {
    
            var returnResult = [];
            for (var i=0; i < string.length; i++) {
                var result = permutation (string[i], string.substr(0, i) + string.substr(i+1));
                for (var j=0; j

    permutation('','123') will return

    ["123", "132", "213", "231", "312", "321"]

提交回复
热议问题