Skip to content

Commit 2f7f7e3

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Sherlock and Anagrams. Solution notes added.
1 parent cc2e791 commit 2f7f7e3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingMedium` `#DictionariesAndHashmaps` `#Strings`
5+
6+
## About solution
7+
8+
To answer the question of "how many pairs" of words can be anagrammed
9+
using fragments from adjacent letters of an initial word, two steps are needed:
10+
11+
1) Obtain all possible fragment candidates to be anagrams,
12+
from each of the possible fragments that can be generated
13+
from adjacent letters of a word.
14+
15+
2) For each list of candidate anagrams,
16+
calculate all possible permutations and add them up.
17+
The total gives the answer.
18+
19+
The second part of this problem can be solved with the binomial coefficient formula:
20+
21+
<https://en.wikipedia.org/wiki/Binomial_coefficient>
22+
23+
But the entire cost of this formula falls on the "factorial" function.
24+
25+
In javascript, the factorial quickly reaches results that return large numbers,
26+
in scientific notation, losing precision.
27+
This loss of precision can result in an erroneous result
28+
in the final calculation of permutations.
29+
30+
To avoid this problem, it is necessary to introduce large number handling using BigInt.

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams-solution-notes.md]]
34
*/
45

56
import { logger as console } from '../../../logger';

0 commit comments

Comments
 (0)