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

前端 未结 8 2136
遥遥无期
遥遥无期 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条回答
  •  孤城傲影
    2020-11-28 10:33

    Well there isnt any built in function in js(i dont believe it to be in any coding language)......and anyways this is the fully functioning program, it omits any repetitions and also displays the number of permutations.....

    var n=0;
    var counter=0;
    var storarr=new Array();
    
    function swap(a,b,str) {        //swaps the terms str[a] and str[b] and returns the final str
            str = str.split("");
            var temp = str[a];
            str[a] = str[b];
            str[b] = temp;
            return str.join("");
    }
    
    function anagram(_a,_b,ar) {        //actual function which produces the anagrams
        if(_a == _b) {
        storarr[n]=ar;
        n++;
        counter++;
        }
        else {
            for(var i= _a;i<= _b;i++) {
                ar=swap(_a,i,ar);
                anagram(_a+1,_b,ar);
                ar=swap(_a,i,ar);
            }
        }
    }
    
    function factorial(a) {         //return a!
        var x=1;
        for(var i=1;i<=a;i++)
        x=x*i;
        return x;
    }
    
    var strl=prompt("Enter String:","");
    var l=strl.length;
    anagram(0,l-1,strl);
    storarr.sort();             //sorts the storarr alphabetically
    var storlen=storarr.length;
    var cai=0;
    var counterarr = new Array();
    strl.split("");
    
    for(var i=0;i");
    
    for(var i=0;i");
    }
    
    strl.join("");
    

提交回复
热议问题