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

前端 未结 8 2133
遥遥无期
遥遥无期 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:53

    function swap(a, b, str) {
      if (a == b)
        str = str;
    
      else {
        str = str.split("");
        var temp = str[a];
        str[a] = str[b];
        str[b] = temp;
        str = str.join("");
      }
    }
    
    function anagram(a1, b1, ar) {
      if (a1 == b1)
        document.write(ar + "
    "); else for (i = a1; i < b1; i++) { swap(a1, b1, ar); anagram((a1) ++, b1, ar); swap(a1, b1, ar); } }

提交回复
热议问题