<script>
function
fact(n)
{
let result = 1;
for
(let i = 1; i <= n; i++)
result *= i;
return
result;
}
function
getResult(str, ch)
{
let has =
new
Array(26).fill(
null
);
for
(let i = 0; i < str.length; i++)
has[str.charCodeAt(i) -
'A'
.charCodeAt(0)]++;
particular = has[ch.charCodeAt(0) -
'A'
.charCodeAt(0)];
if
(particular == 0)
return
0;
has[ch.charCodeAt(0) - 'A
'.charCodeAt(0)] = 0;
// Total length
// of the string
let total = str.length;
// Assume all occurrences of
// particular character as a
// single character.
total = total - particular + 1;
// Compute factorial of the length
let result = fact(total);
// Divide by the factorials of
// the no. of occurrences of all
// the characters.
for (let i = 0; i < 26; i++)
{
if (has[i] > 1)
{
result = result / fact(has[i]);
}
}
// return the result
return result;
}
// Driver Code
let str = "MISSISSIPPI";
// Assuming the string and the character
// are all in uppercase
document.write(getResult(str, '
S') +
"<br>"
);
</script>