File tree Expand file tree Collapse file tree 1 file changed +10
-9
lines changed
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Original file line number Diff line number Diff line change 1
1
# @link Problem definition
2
2
# [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/sherlock_and_anagrams.md]]
3
3
4
+ from typing import Dict , List
4
5
import math
5
6
6
7
7
8
def sherlock_and_anagrams (s_word : str ) -> int :
8
9
9
- candidates = {}
10
- size = len (s_word )
10
+ candidates : Dict [ str , List [ str ]] = {}
11
+ size : int = len (s_word )
11
12
12
13
# Calculate all substrings
13
14
for i in range (0 , size ):
@@ -25,18 +26,18 @@ def sherlock_and_anagrams(s_word: str) -> int:
25
26
else :
26
27
candidates [anagram_candidate ] = [substr ]
27
28
28
- count = 0
29
+ count : int = 0
29
30
# 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 ])
32
33
k = 2
33
34
34
- if len ( candidates [ i ]) <= 1 :
35
- del candidates [i ]
35
+ if quantity_of_anagrams <= 1 :
36
+ del candidates [word ]
36
37
else :
37
38
# 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 )
40
41
)
41
42
42
43
print (f'filtered candidates: { count } ' )
You can’t perform that action at this time.
0 commit comments