Skip to content

Commit ec3c021

Browse files
author
Alison Spittel
committed
4/17 Problems
1 parent e70408b commit ec3c021

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

arrays/count_differences.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

arrays/ransom_note.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

0 commit comments

Comments
 (0)