-
Notifications
You must be signed in to change notification settings - Fork 0
/
002-Valid-Anagram.py
51 lines (40 loc) · 1.71 KB
/
002-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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Link: https://leetcode.com/problems/valid-anagram/description/
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
countS, countT = {}, {}
for i in range(len(s)):
countS[i] = 1 + countS.get(s[i], 0)
countT[i] = 1 + countS.get(t[i], 0)
for c in countS:
if countS[c] != countT.get(c, 0):
return False '''Time Complexity: O(n)
Space Complexity: O(n)'''
return True
------------------------------------------------------------------------------------------------------------------------------------------------------------
# Another approach
return Counter(s) == Counter(t) '''Time Complexity: O(n log n) due to sorting.
or, Space Complexity: O(n)'''
return sorter(s) == sorted(t)
------------------------------------------------------------------------------------------------------------------------------------------------------------
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
counter = {}
for c in s:
if c not in counter:
counter[c] = 1
else:
counter[c] += 1
for c in t:
if c not in counter or counter[c] == 0:
return False
counter[c] -= 1
return True
solution = Solution()
s = "anagram"
t = "nagaram" '''Time Complexity: O(n)
Space Complexity: O(n)'''
print(solution.isAnagram(s, t))