Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Approach 1: sorting and compare
- class Solution:
- def isAnagram(self, s: str, t: str) -> bool:
- return "".join(sorted(s)) == "".join(sorted(t))
- # Approach 2: hashing and compare
- class Solution:
- def isAnagram(self, s: str, t: str) -> bool:
- if len(s)!=len(t):
- return False
- from collections import defaultdict
- hash_s = defaultdict(int)
- hash_t = defaultdict(int)
- for i in range(len(s)):
- hash_s[s[i]] += 1
- hash_t[t[i]] += 1
- return hash_s == hash_t
Advertisement
Add Comment
Please, Sign In to add comment