smj007

Untitled

Jul 30th, 2023
1,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # Approach 1: sorting and compare
  2. class Solution:
  3.     def isAnagram(self, s: str, t: str) -> bool:
  4.         return "".join(sorted(s)) == "".join(sorted(t))
  5.  
  6. # Approach 2: hashing and compare
  7. class Solution:
  8.     def isAnagram(self, s: str, t: str) -> bool:
  9.  
  10.         if len(s)!=len(t):
  11.             return False
  12.  
  13.         from collections import defaultdict
  14.         hash_s = defaultdict(int)
  15.         hash_t = defaultdict(int)
  16.  
  17.         for i in range(len(s)):
  18.             hash_s[s[i]] += 1
  19.             hash_t[t[i]] += 1
  20.  
  21.         return hash_s == hash_t
Advertisement
Add Comment
Please, Sign In to add comment