How can I create a method of finding the total number of possible combinations using a dynamic format

时间:2019-04-08 13:28:02

标签: javascript php math combinations permutation

I have a randomly generated custom number plate format which is as follows:

ISSIIIISS - Where 'I' is an Integer of 0-9 and 'S' is a string of A-Z

An example plate would be: 4DE2947BN

I am trying to find a way in which to calculate the total number of all possible combinations of this number plate format.

How can I do this and how could I use PHP or JavaScript to solve this problem?

I have some grasp of how you can calculate combinations but I am struggling on how this can be implemented into code, especially with the alphanumeric format that I am trying to calculate.

2 个答案:

答案 0 :(得分:0)

I being a number, there is 10 possibilities.

S being a character, there is 26 possibilities.

The total number of combinations is 10 power (TheNumberOfI) * 26 power (TheNumberOfS)

This can be dynamically solved using a simple function that counts the number of S and the number of I and uses the results in the previous equation

function getNumberOfCombinations(input)
{
    const countI = (input.match(/I/g) || []).length;
    const countS = (input.match(/S/g) || []).length;
    const result =  Math.pow(10, countI) * Math.pow(26, countS);
    return (result);
}

console.log(getNumberOfCombinations("ISSIIIISS"));

答案 1 :(得分:0)

I think it should become clear if you take it a piece at a time.
For the first "I", there are 10 possibilities, and for the first "S", there are 26,
so for those two characters together, the total number of possibilities is 10 x 26 = 260.

Then for the next "S", you have 26 possibilities, so the first three characters together have 10 x 26 x 26 = 6760 possibilities.

The pattern continues until you get 26^4 * 10^5 = 45,697,600,000, which in JS is
26**4 * 10**4 or

let x = Math.pow(26,4) * Math.pow(10,5);
console.log(x);