File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments