Get string permutation - Node.js String

Node.js examples for String:String Value

Description

Get string permutation

Demo Code

String.prototype.swap = function(a, b) {
    var array = this.split('');
    var tmp = array[a];
    array[a] = array[b];/*from   ww  w.j  ava2 s  . c om*/
    array[b] = tmp;
    return array.join('');
}

function permutation(str, start) {
    if (start == str.length - 1) {
        console.log(str);
    } else {
        for (var i = start; i < str.length; i++) {
          permutation(str.swap(start, i), start+1);
       }
   }
}

permutation('abc', 0);

Related Tutorials