I am trying to solve it with maths.
I guess I can find answer without actually building any permutations, just knowing number of repeating characters. I built object for in the code, I hope idea can be gathered from it’s structure.
I know that number of total permutations can be calculated with factorial.
But I am struugling to develop formula for this particular task, though I see some patterns.
I did some tests, from that it should be clear where I am in solving this right now.
Note: I know how to solve it by brute force: built array af all perms (I did that recursively in Python some time ago). Then filter it, checking every element for not having repeats. But that would be heavy stuff.
const factorial = (num) => {
let mult = num - 1;
while (mult > 0) {
num *= mult;
mult--;
}
return num;
}
function permAlone(str) {
const len = str.length;
let charOccurances = {};
for (let i = 0; i < len; i++) {
if (charOccurances.hasOwnProperty(str[i])) {
charOccurances[str[i]] += 1;
}
else {
charOccurances[str[i]] = 1;
}
}
console.log('Checking occurances of chars for string ', str);
console.log(charOccurances);
const allPossiblePerms = factorial(len);
console.log('logging number of all perms ', allPossiblePerms);
return str;
}
const testCases = [
["aab", 2],
["aaa", 0],
["aabb", 8],
["abcdefa", 3600],
["abfdefa", 2640],
["zzzzzzzz", 0],
["a", 1],
["aaab", 0],
["aaabb", 12],
];
for (test of testCases) {
console.log('TEST CASE ', test[0]);
console.log('result: ', permAlone(test[0]));
console.log('no repeats perms, expected: ', test[1]);
console.log('---------------')
}
/* TESTS OUTPUT
Output:
TEST CASE aab
Checking occurances of chars for string aab
{ a: 2, b: 1 }
logging number of all perms 6
result: aab
no repeats perms, expected: 2
---------------
TEST CASE aaa
Checking occurances of chars for string aaa
{ a: 3 }
logging number of all perms 6
result: aaa
no repeats perms, expected: 0
---------------
TEST CASE aabb
Checking occurances of chars for string aabb
{ a: 2, b: 2 }
logging number of all perms 24
result: aabb
no repeats perms, expected: 8
---------------
TEST CASE abcdefa
Checking occurances of chars for string abcdefa
{ a: 2, b: 1, c: 1, d: 1, e: 1, f: 1 }
logging number of all perms 5040
result: abcdefa
no repeats perms, expected: 3600
---------------
TEST CASE abfdefa
Checking occurances of chars for string abfdefa
{ a: 2, b: 1, f: 2, d: 1, e: 1 }
logging number of all perms 5040
result: abfdefa
no repeats perms, expected: 2640
---------------
TEST CASE zzzzzzzz
Checking occurances of chars for string zzzzzzzz
{ z: 8 }
logging number of all perms 40320
result: zzzzzzzz
no repeats perms, expected: 0
---------------
TEST CASE a
Checking occurances of chars for string a
{ a: 1 }
logging number of all perms 1
result: a
no repeats perms, expected: 1
---------------
TEST CASE aaab
Checking occurances of chars for string aaab
{ a: 3, b: 1 }
logging number of all perms 24
result: aaab
no repeats perms, expected: 0
---------------
TEST CASE aaabb
Checking occurances of chars for string aaabb
{ a: 3, b: 2 }
logging number of all perms 120
result: aaabb
no repeats perms, expected: 12
---------------
*/