File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Count the number of changes needed to make two words anagrams of one another.
3+
4+ https://www.hackerrank.com/challenges/ctci-making-anagrams
5+ """
6+ from collections import Counter
7+
8+ def number_needed (a , b ):
9+ # O(N) Complexity
10+ a_counter = Counter (a )
11+ b_counter = Counter (b )
12+
13+ differences = 0
14+
15+ for l in a_counter :
16+ if l in b_counter :
17+ if a_counter [l ] != b_counter [l ]:
18+ differences += abs (a_counter [l ] - b_counter [l ])
19+ else :
20+ differences += a_counter [l ]
21+
22+ for l in b_counter :
23+ if l not in a_counter :
24+ differences += b_counter [l ]
25+
26+ return differences
Original file line number Diff line number Diff line change 1+ """
2+ See if a word is available in the magazine to use for a ransom note.
3+
4+ https://www.hackerrank.com/challenges/ctci-ransom-note
5+ """
6+ from collections import Counter
7+
8+ def ransom_note (magazine , ransom ):
9+ magazine_counter = Counter (magazine )
10+ ransom_counter = Counter (ransom )
11+ for word in ransom_counter :
12+ if word not in magazine :
13+ return False
14+ elif ransom_counter [word ] > magazine_counter [word ]:
15+ return False
16+ return True
You can’t perform that action at this time.
0 commit comments