Skip to content

Commit 6e87cab

Browse files
committed
Valid Anagram
1 parent 5882bc8 commit 6e87cab

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

242-valid-anagram.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
Follow up:
1818
What if the inputs contain unicode characters? How would you adapt your solution to such case?
1919
"""
20-
import collections
21-
class Solution(object):
22-
def isAnagram(self, s, t):
20+
# Time Complexity - O(N)
21+
# Space Complexity - O(1) Because maximum size for letters dict is 26
22+
class Solution:
23+
def isAnagram(self, s: str, t: str) -> bool:
2324
if len(s) != len(t):
2425
return False
25-
letters = collections.defaultdict(int)
26+
letters = {}
2627
for c in s:
27-
letters[c] += 1
28+
letters[c] = letters.get(c,0) + 1
2829
for c in t:
29-
if letters[c] <= 0:
30+
if letters.get(c,0) <= 0:
3031
return False
3132
letters[c] -= 1
3233
return True

0 commit comments

Comments
 (0)