Mohannad.ElQadi
BAN USERI think this is a mathematical problem, what we need to do is to calculate all the number of possibilities, remove from it the occurrences of the not allowed doubles and triples.
Here is a simple Javascript code that may fix the problem.
The complexity of it is O(1)
Test cases were covered, n=1, n=2, n=3, n=4
n=1 => the output is 3
n=2 => the output is 8
n=3 => the output is 19
n=4 => the output is 39
let n = 4;
const buildingBlocksArray = ['a', 'b', 'c'];
let countTheNumberOfStrings = (wordsLength, buildingBlocksLength) => {
if(wordsLength === 1){
return buildingBlocksLength;
}
let getCountOfAllPoss = (wordsLength) => {
const countOfAll = Math.pow(buildingBlocksLength, wordsLength);
return countOfAll;
}
let countOfAll = getCountOfAllPoss(wordsLength);
let countOfDoubles = getCountOfAllPoss(wordsLength - 1) * (wordsLength - 1);
let countOfTriples = getCountOfAllPoss(wordsLength - 2) * (wordsLength - 2);
let notAllowedDoubles = countOfDoubles / 3;
let allowedTriples = countOfTriples / 3;
console.log('countOfAll =', countOfAll);
console.log('countOfDoubles =', countOfDoubles);
console.log('countOfTriples =', countOfTriples);
console.log('notAllowedDoubles =', notAllowedDoubles);
console.log('allowedTriples =', allowedTriples);
let finalResults = (countOfAll - notAllowedDoubles - countOfTriples) + allowedTriples;
if(wordsLength <= buildingBlocksLength){
return finalResults;
} else {
return finalResults - (buildingBlocksLength * (wordsLength - buildingBlocksLength))
}
}
console.log('finalResults =', countTheNumberOfStrings(n, buildingBlocksArray.length));
Repcarlotamdaniel, abc at ADP
This is my solution
It has a complexity of O(H * CK) where H is the number of the items in the given hash key and CK is the maximum number of words frequency across all the hashes
- Mohannad.ElQadi October 22, 2019