0

I have the following string: '12345' (the string could also be 'abcd' but I'm using numbers here for the sake of simplification).

Goal: I would like to display all the possible combinations of the characters in the string where the max length of each combination is predetermined.

For example let's take the case of a maximum length of 3 characters.

The output would display the following (it doesn't matter is which forum, it could be an array or anything else):
"1", "2", "3", "4", "5",
"11", "11", "13", "14", "15",
"21", "22", "23", "24", "25"
"31", "32", "33", "34", "35"
"41", "42", "43", "44", "45"
"51", "52", "53", "54", "55"
"111", "112", "113", "114", "115"
"121", "122", "123", "124", "125"
...
"551" "552", "553", "554", "555"

What is the right approach (pseudo code?) to achieve this task? I write in JS but the language of execution is not quite important for me.

Spent days trying to solve this without any success :-(

I managed to figure out a way to calculate the total amount of the output combinations.
- In my example case, where the string is '12345' and the max length of character-combination output is 3... the total is 155 using the following formula:
5^1 + 5^2 + 5^3 = 155

Any help towards accomplishing this task would be greatly appreciated!

FLVR
  • 59
  • 8

2 Answers2

1

Use a recursive function to iterate through each number of characters that you want enumerated. This simply outputs your results to the console and stores them in an array:

var arr = [];
var index = 0;

function recursive(istr,curstr,count) {
  count--;
  for(var i=0; i<istr.length; i++) {
    var str = curstr + istr.charAt(i);
    if(count>0) {
      recursive(istr,str,count);
    }
    else {
      console.log(str);    // showing answers here
      arr[index++] = str;  // or they are in the array here
    }
  }
}

function enumerate(str, n) {
  for(var i=0;i<n;i++) {
    recursive(str,"",i+1);
  }
} 

enumerate("12345",3);

jsfiddle is here: https://jsfiddle.net/FrancisMacDougall/3p0x4ds5/

fmacdee
  • 2,353
  • 10
  • 15
0

There are two algorithm functions need to be used in that algorithm,

If I am not correct, please let me know.

1 st generate all combination of string. I rewrite that one which original is python code https://github.com/jeremykid/FunAlgorithm/blob/master/getMinNumberByCombinationArrays/solution.py.

var getAllStringCombination = function(inputList, returnList, newItem){
if (inputList.length == 0){
    returnList.push(newItem);
    return returnList;
}else{
    for (let i = 0;i<inputList.length;i++){
        let tempList = inputList.slice();
        let tempI = tempList[i];
        tempList.splice(tempList.indexOf(tempList[i]),1);
        getAllStringCombination(tempList,returnList,newItem+tempI);
    }
}
}

2 nd get all subset of one array. get reference from Find all possible subset combos in an array?

var binarySubset = function(inputList){
var res = []
for (let i = 0; i < Math.pow(2, inputList.length); i++) {
    let bin = (i).toString(2), set = [];
    bin = new Array((inputList.length-bin.length)+1).join("0")+bin;
    for (let j = 0; j < bin.length; j++) {
        if (bin[j] === "1") {
            set.push(inputList[j]);
        }
    }
    res.push(set);
}
return res;
}

inputList should be a array, eg: ["1","2","3","4","5"]

Community
  • 1
  • 1
Weijie Sun
  • 194
  • 6