Skip to content

Commit e227130

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Sherlock and Anagrams. Clean Code: better variable naming.
1 parent 3947177 commit e227130

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# @link Problem definition
22
# [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.md]]
33

4+
from typing import Dict, List
45
import math
56

67

78
def sherlock_and_anagrams(s_word: str) -> int:
89

9-
candidates = {}
10-
size = len(s_word)
10+
candidates: Dict[str, List[str]] = {}
11+
size: int = len(s_word)
1112

1213
# Calculate all substrings
1314
for i in range(0, size):
@@ -25,18 +26,18 @@ def sherlock_and_anagrams(s_word: str) -> int:
2526
else:
2627
candidates[anagram_candidate] = [substr]
2728

28-
count = 0
29+
count: int = 0
2930
# Final Anagram list
30-
for i in list(candidates):
31-
total = len(candidates[i])
31+
for word in list(candidates):
32+
quantity_of_anagrams = len(candidates[word])
3233
k = 2
3334

34-
if len(candidates[i]) <= 1:
35-
del candidates[i]
35+
if quantity_of_anagrams <= 1:
36+
del candidates[word]
3637
else:
3738
# Binomial coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient
38-
count += math.factorial(total) // (
39-
math.factorial(k) * math.factorial(total - k)
39+
count += math.factorial(quantity_of_anagrams) // (
40+
math.factorial(k) * math.factorial(quantity_of_anagrams - k)
4041
)
4142

4243
print(f'filtered candidates: {count}')

0 commit comments

Comments
 (0)