File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-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+ if len (s ) != len (t ):
4+ return False
5+
6+ count_t = {}
7+ count_s = {}
8+ for i in range (len (t )):
9+ count_t [t [i ]] = count_t .get (t [i ], 0 ) + 1
10+ count_s [s [i ]] = count_s .get (s [i ], 0 ) + 1
11+
12+ for key in count_s .keys ():
13+ if count_t .get (key ) == None or count_t [key ] != count_s [key ]:
14+ return False
15+
16+ return True
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
3+ grp_anagrams = {}
4+
5+ for string in strs :
6+ count = [0 ] * 26
7+
8+ for char in string :
9+ count [ord (char ) - ord ("a" )] += 1
10+
11+ count = tuple (count )
12+
13+ if grp_anagrams .get (count ) == None :
14+ grp_anagrams [count ] = []
15+
16+ grp_anagrams [count ].append (string )
17+
18+ return grp_anagrams .values ()
You can’t perform that action at this time.
0 commit comments