Skip to content

Commit 2a70101

Browse files
week 2: valid anagram
1 parent 12faf10 commit 2a70101

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

valid-anagram/prograsshopper.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
'''
4+
sol 1
5+
Runtime: Beats 78.74% / O(N)
6+
Memory: Beats 64.30% / O(1)
7+
'''
8+
s_dict = dict()
9+
for elem in s:
10+
try:
11+
s_dict[elem] += 1
12+
except Exception as ex:
13+
s_dict[elem] = 1
14+
for elem in t:
15+
try:
16+
s_dict[elem] -= 1
17+
except Exception as ex:
18+
return False
19+
return not (any(s_dict.values()))
20+
21+
'''
22+
sol 2
23+
Runtime: Beats 72.12% / O(N)
24+
Memory: Beats 97.51% / O(1)
25+
'''
26+
from itertools import zip_longest
27+
from collections import defaultdict
28+
29+
ana_dict = defaultdict(int)
30+
for elem1, elem2 in zip_longest(s, t):
31+
ana_dict[elem1] += 1
32+
ana_dict[elem2] -= 1
33+
return not (any(ana_dict.values()))
34+
35+

0 commit comments

Comments
 (0)