Permutation list

I am trying to list all three permutations of letters, and this is the code I have -

  window.permute = function(){
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    var searchTerm ="aaa";
    var position = 2; 
    changeString(searchTerm, position); 
}

window.changeString = function(searchTerm, position){
    if (position <0){
        alert(newString);

    return; 
    }
    var alphabet = "abcdefghijklmnopqrstuvwxyz"
    for (j=0; j < 26;j++){
        var newString = searchTerm.substr(0, position) + alphabet[j] + searchTerm.substr(position+1);
        var newPosition = position -1; 
        changeString(newString,newPosition);
    }
    return;
}

This does not work, and I'm not sure why - can anyone help?

+5
source share
6 answers
var permutate = (function() {
    var results = [];    
    function doPermute(input, output, used, size, level) {        
        if (size == level) {
            var word = output.join('');
            results.push(word);
            return;
        } 
        level++;
        for (var i = 0; i < input.length; i++) {
            if (used[i]) {
                continue;
            }            
            used[i] = true;
            output.push(input[i]);
            doPermute(input, output, used, size, level);
            used[i] = false;
            output.pop();
        }
    }

    return {
        getPermutations: function(input, size) {
            var chars = input.split('');
            var output = [];
            var used = new Array(chars.length);      
            doPermute(chars, output, used, size, 0);        
            return results;    
        }
    }
})();

for more information visit http://jinwolf.tumblr.com/post/26476479113/draw-something-cheat for a working example, check this jsfiddle http://jsfiddle.net/jinwolf/Ek4N5/31/

+4
source
alert(newString);

newStringnot defined right there. Instead, you should use the argument passed:

alert(searchTerm);

: . . , . , , , , . substr.

http://jsfiddle.net/NUG2A/2/

var alphabet = "abc"; // shortened to save time

function permute(text) {
    if(text.length === 3) { // if length is 3, combination is valid; alert
        console.log(text); // or alert
    } else {
        var newalphabet = alphabet.split("").filter(function(v) {
            return text.indexOf(v) === -1;
        }); // construct a new alphabet of characters that are not used yet
            // because each letter may only occur once in each combination

        for(var i = 0; i < newalphabet.length; i++) {
            permute(text + newalphabet[i]); // call permute with current text + new
                                            // letter from filtered alphabet
        }
    }
}

permute("");

:

permute("");
permute("a");
permute("ab");
permute("abc"); // alert
permute("ac");
permute("acb"); // alert
permute("b");
// ...
+1

, "", , , "aaa".

, . , , , pimvdb .

: , :

  • (aaa, aab,...): n ^ k = 26 ^ 3 = 17,576
  • (abc, bac,...): n!/(n-k)!= 26!/(26-3)!= 15 600
+1
for (j=0; j < 26;j++){

for (var j=0; j<26; j++) {

Without a declaration, it jis a global variable, so 26only one iteration is required to go to , and then all loops complete.

0
source

For permutations, the recursive algorithm shown as pimvd is always good, but don't forget that you can just overdo it with for-loops when N is small:

for(int x1=0; x1 < 26; x1++)
for(int x2=0; x2 < 26; x2++)
for(int x3=0; x3 < 26; x3++){
    //do something with x1, x2, x3
}
0
source

In C #:

    void DoPermuation(string s)
    {
        var pool = new HashSet<string>();
        //Permute("", , pool);
        pool = Permute(new List<char>(s));
        int i = 0;
        foreach (var item in pool) Console.WriteLine("{0:D2}: {1}", ++i, item);      
    }

    HashSet<string> Permute(List<char> range)
    {
        if (range.Count == 1) return new HashSet<string>(new string[] { range[0].ToString() });

        var pool = new HashSet<string>();
        foreach (var c in range)
        {             
            var list = new List<char>(range);
            list.Remove(c);
            foreach (var item in Permute(list)) pool.Add(c + item);                
        }

        return pool;
    }
-3
source

All Articles