-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Anagram.py
36 lines (27 loc) · 863 Bytes
/
Valid Anagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## Time complexity - O(NMlogNM) Space Complexity - O(N+M)
## This is because the sorted strings are stored in memory, and the space required is proportional to the length of the strings.
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s = sorted(s)
t = sorted(t)
if s == t:
return True
return False
## Time complexity - O(N+M) Space Complexity - O(N+M)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
hash_s = {}
hash_t = {}
for i in s:
if i not in hash_s:
hash_s[i] = 1
else:
hash_s[i] += 1
for i in t:
if i not in hash_t:
hash_t[i] = 1
else:
hash_t[i] += 1
if hash_t == hash_s:
return True
return False