Skip to content

Commit bc139ae

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Sherlock and Anagrams. Fixed ✅. Solve all test cases.
Precision bug in big results from "factorial()" fixed using BigInt. https://stackoverflow.com/a/55235555/6366150
1 parent ee6564d commit bc139ae

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.md]]
33
*/
44

5-
function factorial(n) {
6-
if (n === 0) {
7-
return 1;
8-
}
9-
return n * factorial(n - 1);
5+
import { logger as console } from '../../../logger.js';
6+
7+
function extraLongFactorials(n) {
8+
const rs = [...Array(n)].reduce((a, b, i) => a * BigInt(i + 1), 1n);
9+
return rs;
1010
}
1111

1212
export function sherlockAndAnagrams(s) {
@@ -16,6 +16,9 @@ export function sherlockAndAnagrams(s) {
1616
for (let i = 0; i < size; i++) {
1717
for (let j = 0; j < size - i; j++) {
1818
const substr = s.substring(i, size - j);
19+
console.debug(
20+
`i: ${i}, size: ${size}, size - j: ${size - j} | substr: ${substr}`
21+
);
1922

2023
// Add substrings to a candidate list.
2124
// two strings are anagrams if sorted strings are the same.
@@ -32,7 +35,8 @@ export function sherlockAndAnagrams(s) {
3235
}
3336
}
3437

35-
let count = 0;
38+
let total = BigInt(0);
39+
let qCandidates = 0;
3640
// Final Anagram list
3741
for (const word of Object.keys(candidates)) {
3842
const quantityOfAnagrams = candidates[word].length;
@@ -42,15 +46,22 @@ export function sherlockAndAnagrams(s) {
4246
delete candidates[word];
4347
} else {
4448
// Binomial coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient
45-
count += Math.floor(
46-
factorial(quantityOfAnagrams) /
47-
(factorial(k) * factorial(quantityOfAnagrams - k))
48-
);
49+
qCandidates += quantityOfAnagrams;
50+
51+
const count =
52+
extraLongFactorials(quantityOfAnagrams) /
53+
(extraLongFactorials(k) * extraLongFactorials(quantityOfAnagrams - k));
54+
total += count;
55+
56+
console.debug(`'Partial anagrams of ${word}: ${count}`);
4957
}
5058
}
51-
console.debug(`filtered candidates: ${count}`);
59+
console.debug(
60+
`'sherlockAndAnagrams(${s}) Filtered # candidates: ${qCandidates}`
61+
);
62+
console.debug(`'sherlockAndAnagrams(${s}) # anagrams: ${total}`);
5263

53-
return count;
64+
return Number(total);
5465
}
5566

5667
export default { sherlockAndAnagrams };

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TEST_CASES from './sherlock_and_anagrams.testcases.json';
66

77
describe('sherlock_and_anagrams', () => {
88
it('sherlockAndAnagrams test cases', () => {
9-
expect.assertions(5);
9+
expect.assertions(15);
1010

1111
TEST_CASES.forEach((testSet) => {
1212
testSet.tests.forEach((test) => {

0 commit comments

Comments
 (0)