Skip to content Skip to sidebar Skip to footer

Generate All Combination And Permutation Of Supplied Word

How can I get all possible combination of words using javascript? For example - if I have 3 word Apple , Banana , Orange I need all unique combinations of those words I.e. comb1 =

Solution 1:

A more efficient way of generating the permutations

functionpermutations(k, curr_perm, included_items_hash){
     if(k==0){
        perm.push(curr_perm);
        return;
     }
     
     
     for(let i=0; i<items.length; i++){

        // so that we do not repeat the item, using an array here makes it O(1) operationif(!included_items_hash[i]){
            included_items_hash[i] = true;
            permutations(k-1, curr_perm+items[i], included_items_hash);
            included_items_hash[i] = false;
        }
        
     }
 }

let items = ["a","b","c"];
let perm = [];

// for generating different lengths of permutationsfor(let i=0; i<items.length; i++){
    permutations(items.length-i, "", []);
}

console.log(perm);

Post a Comment for "Generate All Combination And Permutation Of Supplied Word"